Person と呼ばれる、作成した単純なクラスのリストが必要なユーザーコントロールがあります。
public class Person {
public string Name { get; set; }
}
ユーザーコントロールでは、バインドできる ObservableCollection<Person> が必要です。したがって、依存関係プロパティにする必要があると思います。したがって、ユーザーコントロールには次のものがあります。
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<Person> を作成し、メインウィンドウのデータ コンテキストを自分自身に設定して、次のようにバインドします。
<Local:PersonUserControl Persons="{Binding PersonList}">
</Local:PersonUserControl>
そして、エラーが発生します:呼び出しのターゲットによって例外がスローされました。- これ以上の説明はありません。それにどう反応するか、または私が間違っていることを誰か教えてもらえますか。
私が十分に明確であることを願っています。