【发布时间】:2013-01-19 21:30:10
【问题描述】:
我有一个用户控件,我需要一个我创建的简单类的列表,称为 Person:
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 代码隐藏中,我创建了一个名为 PersonList 的 ObservableCollection
<Local:PersonUserControl Persons="{Binding PersonList}">
</Local:PersonUserControl>
我得到了错误:调用的目标抛出了异常。 - 没有进一步的解释。谁能告诉我如何应对它或我做错了什么。
我希望我说得够清楚了。
【问题讨论】:
-
您能否发布堆栈跟踪以及内部异常(如果有)?顺便说一句,您应该将您的财产命名为
PersonsProperty,所以它应该是public static readonly DependencyProperty PersonsDependencyProperty -
能否请您出示您的 xaml.cs 文件代码
标签: wpf binding user-controls dependency-properties invocationtargetexception