加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Datagrid RowDetails未更新

发布时间:2020-12-15 21:42:35 所属栏目:百科 来源:网络整理
导读:我在修改DataDrid绑定的集合(“Items”)时更新RowDetailsTemplate时遇到问题.正在视图模型中修改集合.当我修改其中一个绑定项的内容时,更改将在DataGridRow和RowDetailsTemplate中更新.例如. Items[i].Name = "new name"; // RowDetailsTemplate gets update
我在修改DataDrid绑定的集合(“Items”)时更新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,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"});

会产生同样的效果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读