当 ObservableCollection 值更新时,WPF 数据网格绑定不会更新 [英] WPF Datagrid Bindings Not Updating When ObservableCollection Values Updated

查看:86
本文介绍了当 ObservableCollection 值更新时,WPF 数据网格绑定不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF DataGrid 绑定到一个名为Personnel"的 ObservableCollection.我在 DataGrid 中有一个可编辑的 DataGridCheckBoxColumn.CheckBoxColumn 绑定到我的集合中名为AircraftCommanderSelected"的布尔值.选择行并选中复选框后,将触发事件以更新集合,以便每个人员"的所有飞行器组成的值都设置为false(仅设置为true的字母除外).话虽如此,我的集合正在正确更新,但我的数据网格不会取消选中"先前选中的框,其绑定值已更改为 false.如何通知值已更改?下面是我的代码(为了更容易阅读而修改).

I have a WPF DataGrid bound to an ObservableCollection called "Personnel". I have a DataGridCheckBoxColumn in the DataGrid that is editable. The CheckBoxColumn is bound to a bool value called "AircraftCommanderSelected" in my collection. When a row is selected and the checkbox is checked, an event is fired to update the collection so that all AircraftCommanderSelected values for each "Personnel" are set to false (except for the one that was just set to true). With that said, my collection is updating correctly, but my datagrid will not 'uncheck' the previously checked boxes, who's bound value has been changed to false. How can I notify that the value has been changed? Below is my code (modified for easier reading).

 public class Personnel
 {
      ///
      ///Other objects removed for reading purposes. All follow same format.
      ///
      private bool aircraftCommanderSelected;
      public bool AircrafCommanderSelected
      {
            get { return this.aircraftCommanderSelected; }
            set
            {
                if(this.aircraftCommanderSelected != value)
                {
                     this.aircraftCommanderSelected = value;
                     this.NotifyPropertyChanged("AircraftCommanderSelected");
                }
            }
      }

      public event PropertyChangedEventHandler PropertyChanged;
      public void NotifyPropertyChanged(strin propName)
      {
           if(this.PropertyChanged != null)
           {
               this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
           }
      }
 }

XAML

 <DataGrid Name="dataGrid" AutoGenerateColumns="False" SelectedItem="{Binding Personnel}" CanUserDeleteRows="False" CanUserAddRows="False" IsReadOnly="False" SelectionMode="Single" CellEditEnding="dataGrid_CellEditEnding">
     <DataGrid.Columns>
         <DataGridCheckBoxColumn local:DataGridUtil.Name="ac" Header="AC" Binding="{Binding AircraftCommanderSelected}"/>
     </DataGrid.Columns>
 </DataGrid>

背后的代码

 private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
 {
         foreach (Personnel p in vm.personnel)//Loop through each item in the collection
         {
             //per.ID is the ID of the person who was just edited.
             //This sets everyones value to false except the person that was just edited.
             if (p.ID != per.ID) { p.AircraftCommanderSelected = false; }
         }
  }

当集合被修改并触发属性更改事件时,数据网格不应该更新吗?

When the collection is modified and the property changed event is fired, shouldn't the datagrid update?

我找到了一个解决方案,但它涉及多线程,这似乎是解决此问题的不当方法.我也不喜欢它如何刷新整个网格并取消选择我当前的选择

I have found a solution but it involves multithreading, which seems like an improper solution to this problem. I also don't like how it refreshes the whole grid and deselects my current selection

 dataGrid.Dispatcher.BeginInvoke(new Action(() => dataGrid.Items.Refresh()), System.Windows.Threading.DispatcherPriority.Background);

感谢任何帮助.

谢谢

-贾斯汀

推荐答案

您需要检查一系列事项:

There's a series of things you need to check:

  • ObservableCollection 中的对象是否正确实现了 INotifyPropertyChanged?(不在它发布的代码中)
  • 属性名称是否有拼写错误或大小写变化?如果您使用的是 .NET 4.6,您可以使用新的 nameof() 属性来确保这一点.
  • 如果可以从代码隐藏中更改值,则必须使用 Mode=TwoWay
  • Are the objects in your ObservableCollection implementing INotifyPropertyChanged properly? (Not in the code it posted)
  • Are there any misspellings or capitalization changes with the property name? If you are on .NET 4.6 you can use the new nameof() property to make sure of that.
  • If the value can be changed from code-behind, then you must use Mode=TwoWay

默认情况下,大多数绑定是 OneWay.如果您的绑定模式是一种方式并且您更改了后面代码中的值,则绑定将中断并且在您重新加载 XAML 之前不再起作用.

By default most bindings are OneWay. If your binding mode is one way and you change the value in code behind, the binding will break and no longer work until you reload the XAML.

这篇关于当 ObservableCollection 值更新时,WPF 数据网格绑定不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆