2013-11-01 129 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

相关问题