これがどのように機能するのか興味があります.ObservableCollectionのPropertyを持っているMainViewModel
と呼ばれるPropertyを持っているからですSubViewModel
(私たちはそれを と呼びますProperty1
.)
すべてに INotifyChangedProperty を実装しました。
私のメインウィンドウ
<Window ..
DataContext="{Binding MainViewModel}" />
...
<StackPanel DataContext="{Binding SubViewModel}">
<local:SomeControl DataContext="{Binding}" />
</StackPanel>
</Window>
そして私のUserControl
<UserControl Name="SomeControl">
<DataGrid Name="MyDataGrid" ItemSource="{Binding Property1, Mode=TwoWay}" CurrentCellChanged="TestMethod" />
...
</UserControl>
私のテスト方法では、変更がメイン ビュー モデルに反映されない理由を突き止めるためのテストとして、次のようなことを行います。
private void TestMethod()
{
var vm = this.DataContext as SubViewModel;
var itemSourceObservableCollection = MyDataGrid.ItemsSource as ObservableCollection<MyType>;
//I thought vm.Property1 would be equal to itemSourceObservableCollection
//but they are not, itemSourceObservableCollection shows the changes I've made
//vm.Property1 has not reflected any changes made, even though I though they were the same item
}
それで、ItemSource はバインド先のアイテムのコピーを作成する必要があることがわかりましたか? このプロパティが変更され、更新する必要があることをviewModelに手動で通知するにはどうすればよいですか? それが INotifyPropertyChanged の仕事だと思いましたか?
私の問題の一部は、これが内部でどのように機能するかを理解していないことだと思います。私のコードが期待どおりに動作しない理由を理解するのに役立つ良いブログ投稿やドキュメントを誰かが指摘できれば、それは素晴らしいことです。