WPF MVVM DataGridComboboxColumn变化一行全部更新(WPF MVVM D

2019-10-21 23:36发布

我有我的观点一个数据网格。 其中一列是的ItemSource通过静态资源绑定到一个列表在我看来模型datagridcomboboxcolumn。 问题是,当我在一行中改变所选择的项目,同一项目上的其他行选择。

视图模型

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);
   }
}

Answer 1:

这个问题是不是一个真正的问题; 无论你相信与否,它实际上是编程来做到这一点故意(这是一个功能,而不是bug)。

CollectionView ■找一个CurrentItem财产。 如果直接数据绑定到CollectionView ,那么它保持同步的数据。 有每个从继承类的选项Selector类: Selector.IsSynchronizedWithCurrentItem

如果真正 SelectedItem总是与在当前项目同步ItemCollection ; SelectedItem不会与当前项目同步; SelectedItem与仅在当前项目同步Selector使用CollectionView 。 默认值为null。


关键的是,它默认为 ,这对同步CollectionView秒(这是你在数据绑定什么)。 所以,只要将该属性设置为你的组合框假,你都设置。



文章来源: WPF MVVM DataGridComboboxColumn change one row updates all
标签: c# wpf mvvm