WPF MVVM DataGridComboboxColumn 更改一行更新所有 [英] WPF MVVM DataGridComboboxColumn change one row updates all

查看:82
本文介绍了WPF MVVM DataGridComboboxColumn 更改一行更新所有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格.其中一列是 datagridcomboboxcolumn,它的 itemsource 通过静态资源绑定到我的视图模型中的列表.问题是,当我在一行中更改所选项目时,其他行上的相同项目被选中.

I've got a datagrid in my view. One of the columns is a datagridcomboboxcolumn which itemsource is bound to a list in my view model through a static resource. The problem is that when I change the selected item in one row, the same item is selected on the other rows.

查看模型

public class MyViewModel
{
   private ObservableCollection<Wavelength> wlList = new ObservableCollection<Wavelength>();
   private ObservableCollection<Model.AcquisitionParameters> acquisitionList = new ObservableCollection<Model.AcquisitionParameters>();

   public ObservableCollection<Model.AcquisitionParameters> AcquisitionList
   {
      get { return acquisitionList; }
      set { acquisitionList = value; OnPropertyChanged("AcquisitionList"); }
   }

   public ObservableCollection<Model.Wavelength> Wavelengths 
   {
      get { return wavelengths; }
      set { wavelengths = value; } 
   }
}

查看

<UserControl.Resources>
   <CollectionViewSource Source="{Binding Wavelengths}" x:Key="wlList" />
</UserControl.Resources>
<DataGrid AutoGenerateColumns="False" 
                  ItemsSource="{Binding Path=AcquisitionList, UpdateSourceTrigger=PropertyChanged}"                  
                  CanUserAddRows="True" CanUserDeleteRows="True" SelectionUnit="FullRow">
 <DataGrid.Columns>  
   <DataGridComboBoxColumn Header="Wavelength" Width="SizeToHeader"                                        
                                        SelectedValueBinding="{Binding Wavelength}"
                                        SelectedValuePath="Value"
                                        ItemsSource="{Binding Source={StaticResource wlList}}"
                                        EditingElementStyle="{StaticResource StandardComboBox}"
                                        ElementStyle="{StaticResource StandardComboBox}" />
  </DataGrid.Columns>
</DataGrid>

波长

public class Wavelength
{
   private double wavelength;

   public double Value 
   {
      get { return wavelength; }
      set { wavelength = Value; }
   }

   public Wavelength(double wl)
   {
      this.wavelength = wl;
   }

   public override string ToString()
   {
      return string.Format("{0:0} nm", wavelength * 1e9);
   }
}

推荐答案

问题不是真的问题;信不信由你,它实际上被编程为故意这样做(这是一个功能,而不是一个错误).

The problem is not really a problem; believe it or not, it's actually programmed to do this purposely (it's a feature, not a bug).

CollectionViews 有一个 CurrentItem 属性.如果您直接将数据绑定到 CollectionView,那么它会保持数据同步.从 Selector 类:Selector.IsSynchronizedWithCurrentItem.

CollectionViews have a CurrentItem property. If you databind directly to the CollectionView, then it keeps the data synchronized. There is an option on every class that inherits from the Selector class: Selector.IsSynchronizedWithCurrentItem.

true 如果 SelectedItem 始终与 ItemCollection;false 如果 SelectedItem 永远不会与当前项目同步;null 如果 SelectedItem 仅在 Selector 使用 CollectionView.默认值为 null.

true if the SelectedItem is always synchronized with the current item in the ItemCollection; false if the SelectedItem is never synchronized with the current item; null if the SelectedItem is synchronized with the current item only if the Selector uses a CollectionView. The default value is null.

<小时>

关键部分是它默认为 null,它在 CollectionView 上同步(这是您数据绑定到的).因此,只需将组合框上的属性设置为 false 即可.


The key part is that it defaults to null, which synchronizes on CollectionViews (which is what you are databinding to). So, just set the property to false on your combobox and you are all set.

这篇关于WPF MVVM DataGridComboboxColumn 更改一行更新所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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