has*_*ock 5 c# wpf xaml mvvm wpfdatagrid
RowDetailsTemplate在修改 aDataGrid绑定到的集合(“项目”)时,我在获取更新时遇到问题。正在从视图模型中修改集合。当我修改绑定项目之一的内容时,DataGridRow 和 RowDetailsTemplate 中的更改都会更新。例如
Items[i].Name = "new name"; // RowDetailsTemplate gets updated
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其中一项分配给一个全新的对象,DataGridRow 会更新,但 RowDetailsTemplate 不会更新。例如
Items[i] = new Model {Name = "new name"}; // RowDetailsTemplate NOT updated
Run Code Online (Sandbox Code Playgroud)
一开始我唯一想到的是,我需要为绑定的 Items 的 CollectionChanged 事件添加一个侦听器,并显式地引发属性更改通知。例如
Items = new ObeservableCollection<Model>();
Items.CollectionChanged += (o,e) => OnNotifyPropertyChanged("Items");
Run Code Online (Sandbox Code Playgroud)
但这没有用。
我的 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>
Run Code Online (Sandbox Code Playgroud)
为什么会DataGridRow通知已更改的 Item 而不是RowDetailsTemplate?!
更新 执行删除/添加而不是修改集合的工作。例如
Items.Remove(Items[i]);
Items.Add (new Model {Name = "new name"}); // RowDetailsTemplate updated OK
Run Code Online (Sandbox Code Playgroud)
(哦,模型类当然实现了INotifyPropertyChanged。)
似乎这可能是我需要刷新详细信息视图的 DataContext 的问题?
为什么你不能:
Items.RemoveAt(i);
Items.Insert(i,(new Model {Name = "new name"});
Run Code Online (Sandbox Code Playgroud)
会有同样的效果。
| 归档时间: |
|
| 查看次数: |
1238 次 |
| 最近记录: |