[英]Datagrid RowDetails not being updated
我有获取一个问题RowDetailsTemplate
修改集合(“项目”)的时候,一个更新DataGrid
必然。 正在视图模型中修改集合。 of one of the bound items, the change is updated in both the DataGridRow and RowDetailsTemplate. 当我修改其中一个绑定项的时,更改将在DataGridRow和RowDetailsTemplate中更新。 例如
Items[i].Name = "new name"; // RowDetailsTemplate gets updated
但是,如果我将其中一个项目分配给一个全新的对象,DataGridRow会更新,但RowDetailsTemplate不会更新。 例如
Items[i] = new Model {Name = "new name"}; // RowDetailsTemplate NOT updated
我首先想到的唯一事情是我需要为绑定Items的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
通知更改的Item而不是RowDetailsTemplate
?
更新执行删除/添加而不是修改集合。 例如
Items.Remove(Items[i]);
Items.Add (new Model {Name = "new name"}); // RowDetailsTemplate updated OK
(哦,Model类当然实现了INotifyPropertyChanged
。)
似乎这可能是我需要刷新详细信息视图的DataContext的问题?
你为什么不能:
Items.RemoveAt(i);
Items.Insert(i,(new Model {Name = "new name"});
会产生同样的效果。
我不得不在CellEditEnding处理程序代码中插入这样一个肮脏的黑客:
DataTemplate temp = ProfileDataGrid.RowDetailsTemplate;
ProfileDataGrid.RowDetailsTemplate = null;
ProfileDataGrid.RowDetailsTemplate = temp;
它的工作原理,行细节更新,但我也想知道大师如何更新RowDetails。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.