0

我有一个用户控件,我需要我做了一个简单的类的列表,称为联系人:绑定到观察到的集合,依赖属性失败

public class Person { 
    public string Name { get; set; } 
} 
在用户控件

现在,我需要有一个的ObservableCollection <我可以绑定的人>。所以我想我需要使它成为一个依赖属性。所以在用户控件我有以下几点:

public static readonly DependencyProperty PersonsDependencyProperty = 
    DependencyProperty.Register("Persons", typeof(ObservableCollection<Person>), 
           typeof(PersonUserControl)); 

,并且属性是这样的:

public ObservableCollection<Person> Persons 
{ 
    get 
    { 
     return (ObservableCollection<Person>)GetValue(PersonsDependencyProperty); 
    } 
    set 
    { 
     SetValue(PersonsDependencyProperty, value); 
    } 
} 

现在在我的代码隐藏MainWindow.xaml我做一个ObservableCollection <人>称为PersonList,设置主窗口datacontext自我,并像这样绑定:

<Local:PersonUserControl Persons="{Binding PersonList}"> 
</Local:PersonUserControl> 

而且我得到的错误:异常已被抛出的目标一个调用。 - 没有进一步的解释。任何人都可以告诉我如何对此做出反应或者我做错了什么。

我希望我已经够清楚了。

+0

你可以发布堆栈跟踪,也是它的内部异常,如果有的话?顺便说一句,你应该命名你的财产为'PersonsProperty',所以它应该是'公共静态只读DependencyProperty PersonsDependencyProperty' – nemesv

+0

你可以请显示你的xaml.cs文件代码 – ethicallogics

回答

0

PersonsDependencyProperty应该只是PersonsProperty。很难说这是否是没有更多信息的根本原因,但这确实是一个问题。 WPF将“属性”附加到绑定路径中以查找相关的依赖项属性。因此,它不会找到你的。