2013-11-01 128 views
2

我得到了一個包含類型列表的依賴屬性的usercontrol(它位於一個庫中/也試過一個正常的屬性)。綁定屬性到usercontrol

public partial class PicSelection : UserControl 
{ 
    #region Properties 
    public static readonly DependencyProperty LstImagesProperty = DependencyProperty.Register("LstImages", typeof(List<string>), typeof(PicSelection), new FrameworkPropertyMetadata(null)); 

    // .NET Property wrapper 
    public List<string> LstImages 
    { 
     get { return (List<string>)GetValue(LstImagesProperty); } 
     set { SetValue(LstImagesProperty, value); } 
    } 
    #endregion 
    ... 

我也得到了數據類:

public class Data : BaseObject 
{ 
    #region Members 
    public List<string> Images { set { SetValue("Images", value); } get { return (GetValue<List<string>>("Images")); } } 
    #endregion 


    #region Construction 
    public GameData() 
    { 
     Images = new List<string>(); 
     Images.Add("pack://application:,,,/TestApp;component/Content/Images/Pictures/0002.jpg"); 
    } 
    #endregion 
} 

基礎對象用於自動創建Dependance的屬性:

[Serializable] 
public abstract class BaseObject : PropertyNotifier 
{ 
    #region Members 
    private readonly IDictionary<string, object> _values = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase); 
    #endregion 

現在我想的Data.Images綁定到customcontrol。 LstImages(「Data」是使用控件的頁面上的Data數據類型的屬性)。該程序毫無例外地工作,但不知何故在控制中的LstImages,我檢查了幾個事件,始終爲空。

<controls:PicSelection Name="SelPic" LstImages="{Binding Data.Images}" Foreground="White" FontSize="16"/> 

。另一方面,要做到每

<usercontrol SomeArray="{x:Static data:StaticClass.TheStrings}"/> 

與靜態類(涉及組織,幾乎是相同的),同樣的事情,就是這麼簡單。它甚至可以與普通屬性一起使用Datacontext的設置對此沒有任何影響。我忽略了什麼?

+0

什麼在Visual Studio輸出窗口的?你應該在那裏看到綁定錯誤。 – 2013-11-01 12:05:53

回答

0

你看過輸出窗口嗎?通常情況下,如果綁定失敗,你會看到一些錯誤。

您需要檢查傳入PicSelection控件的數據上下文是什麼。你可以嘗試顯式設置數據上下文,然後直接綁定到Images屬性?

LstImages="{Binding Path=Images}" 
+0

這是一個偉大的建議。但是輸出結果也沒有顯示所有內容都已正確加載。 – Edgar

+0

你可以複製輸出窗口中的內容以便我們看到嗎? – 2013-11-01 12:20:38

+1

非常感謝 - 你讓我的錯誤。設置沒有路徑的上下文不起作用。但是將DataContext直接設置爲Data compined with Path works。非常感謝。 – Edgar

0

如果Data實例您UserControlDataContext,那麼你需要更新你的Binding作爲

<controls:PicSelection Name="SelPic" LstImages="{Binding Images}" Foreground="White" FontSize="16"/> 
+0

當然。我試圖將datacontext設置爲「this」(包含Data的頁面)。無論如何,我嘗試這種方式 - 不起作用。 – Edgar

相關問題