WPF MVVM DataGridComboboxColumn change one row upd

2019-08-02 01:23发布

问题:

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.

View Model

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

View

<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>

Wavelength

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

回答1:

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 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 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.


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.



标签: c# wpf mvvm