がバインドされRowDetailsTemplate
ているコレクション (「アイテム」) を変更するときに、 を更新するのに問題があります。DataGrid
コレクションは、ビュー モデル内から変更されています。バインドされたアイテムの 1 つのコンテンツを変更すると、DataGridRow と RowDetailsTemplate の両方で変更が更新されます。例えば
Items[i].Name = "new name"; // RowDetailsTemplate gets updated
しかし、項目の 1 つをまったく新しいオブジェクトに割り当てると、DataGridRow は更新されましたが、RowDetailsTemplate は更新されません。例えば
Items[i] = new Model {Name = "new name"}; // RowDetailsTemplate NOT updated
最初に考えたのは、バインドされたアイテムの CollectionChanged イベントにリスナーを追加し、プロパティの変更通知を明示的に発生させる必要があるということだけでした。例えば
Items = new ObeservableCollection<Model>();
Items.CollectionChanged += (o,e) => OnNotifyPropertyChanged("Items");
しかし、それはうまくいきませんでした。
私の XAML バインディングは次のようになります。
<DataGrid DataContext="{StaticResource viewmodel}"
ItemsSource="{Binding Items, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
DataGridRow
変更されたアイテムが通知されるのに、なぜ通知されないのRowDetailsTemplate
ですか?!
更新 コレクションを変更する代わりに、削除/追加を行います。例えば
Items.Remove(Items[i]);
Items.Add (new Model {Name = "new name"}); // RowDetailsTemplate updated OK
(もちろん、Model クラスは を実装してINotifyPropertyChanged
います。)
これは、詳細ビューの DataContext を更新する必要がある問題のように思えますか?