Wpf 绑定到可观察集合,依赖项属性失败

Wpf 绑定到可观察集合,依赖项属性失败,wpf,binding,user-controls,dependency-properties,invocationtargetexception,Wpf,Binding,User Controls,Dependency Properties,Invocationtargetexception,我有一个usercontrol,其中我需要一个我创建的简单类的列表,名为Person: public class Person { public string Name { get; set; } } 现在在usercontrol中,我需要一个可以绑定的ObservableCollection。所以我想我需要把它变成一个依赖属性。因此,在usercontrol中,我有以下内容: public static readonly DependencyProperty PersonsDepen

我有一个usercontrol,其中我需要一个我创建的简单类的列表,名为Person:

public class Person {
    public string Name { get; set; }
}
现在在usercontrol中,我需要一个可以绑定的ObservableCollection。所以我想我需要把它变成一个依赖属性。因此,在usercontrol中,我有以下内容:

public static readonly DependencyProperty PersonsDependencyProperty =
    DependencyProperty.Register("Persons", typeof(ObservableCollection<Person>),
                                typeof(PersonUserControl));
公共静态只读DependencyProperty人员DependencyProperty=
从属财产登记簿(“人员”),类型(可观察到的收集),
类型(PersonUserControl));
财产是这样的:

public ObservableCollection<Person> Persons
{
   get
    {
        return (ObservableCollection<Person>)GetValue(PersonsDependencyProperty);
    }
    set
    {
        SetValue(PersonsDependencyProperty, value);
    }
}
公众可观察收集人员
{
得到
{
返回(ObservableCollection)GetValue(PersonDependencyProperty);
}
设置
{
设置值(PersonDependencyProperty,value);
}
}
现在,在我的MainWindow.xaml codebehind中,我创建了一个名为PersonList的ObservableCollection,将MainWindow datacontext设置为self,并像这样绑定到它:

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

我得到一个错误:调用的目标抛出了异常。-没有进一步的解释。有谁能告诉我如何应对,或者我做错了什么


我希望我说得够清楚。

PersonsDependencyProperty
应该只是
PersonsProperty
。如果没有更多的信息,很难说这是否是根本原因,但这肯定是个问题。WPF在绑定路径上追加“Property”以查找相关的依赖属性。因此,它将找不到您的。

您可以发布堆栈跟踪以及内部异常(如果有)吗?顺便说一下,您应该将您的属性命名为
PersonsProperty
,因此它应该是
公共静态只读DependencyProperty PersonsDependencyProperty
,您可以显示您的xaml.cs文件代码吗