5

RowDetailsTemplate在修改 aDataGrid绑定到的集合(“项目”)时,我在获取更新时遇到问题。正在从视图模型中修改集合。当我修改其中一个绑定项的内容时,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通知更改的项目而不是RowDetailsTemplate?!

更新 执行删除/添加而不是修改集合的工作。例如

Items.Remove(Items[i]);
Items.Add (new Model {Name = "new name"});  // RowDetailsTemplate updated OK

(哦,模型类当然实现了INotifyPropertyChanged。)

似乎这可能是我需要刷新详细信息视图的 DataContext 的问题?

4

2 回答 2

2

为什么你不能:

Items.RemoveAt(i);
Items.Insert(i,(new Model {Name = "new name"});

会有同样的效果。

于 2012-11-07T21:01:03.433 回答
1

我不得不在 CellEditEnding 处理程序代码中插入这样一个肮脏的黑客:

DataTemplate temp = ProfileDataGrid.RowDetailsTemplate;
ProfileDataGrid.RowDetailsTemplate = null;
ProfileDataGrid.RowDetailsTemplate = temp;

行得通,Row Detail 更新了,但我也想知道大师们是如何更新 RowDetails 的。

于 2015-02-26T13:22:06.677 回答