【问题标题】:WPF MVVM DataGridComboboxColumn change one row updates allWPF MVVM DataGridComboboxColumn 更改一行更新所有
【发布时间】:2015-02-19 22:19:37
【问题描述】:

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

查看模型

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

【问题讨论】:

  • SelectedValueBinding="{Binding Wavelength}" & SelectedValuePath="Value" 可以用 SelectedItemBinding 代替吗?你能发布 AcquisitionParameters 类吗?
  • 我不明白你是如何绑定到 StaticResource wlList 的,因为 wList 在你的 XAML 中不能作为资源使用
  • 我假设您已经从该 xaml 中删除了一堆数据?例如,将 wavelenghtList 缩短为 wlList,省略了 XAML 标记
  • Also SelectedValueBinding with SelectedValuePath 意味着您会将所选 WaveLength 的双精度值绑定到 AcquisitionParameters 上的 WaveLength 属性,我假设它也是 Double?
  • 另外,您确定没有将相同的 Model.AcquisitionParameters 对象添加到集合中吗?

标签: c# wpf mvvm


【解决方案1】:

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

CollectionViews 有一个CurrentItem 属性。如果您直接将数据绑定到CollectionView,那么它将保持数据同步。每个继承自 Selector 类的类都有一个选项:Selector.IsSynchronizedWithCurrentItem

true 如果SelectedItem 始终与ItemCollection 中的当前项同步; false 如果 SelectedItem 从未与当前项目同步; null 如果SelectedItem 与当前项目同步,则仅当Selector 使用CollectionView 时。默认值为 null


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

【讨论】:

  • 谢谢!该解决方案并不完整,因为 DataGridComboBoxColumn 没有 IsSynchronizedWithCurrentItem 属性。解决方法是使用样式设置器:wpf.codeplex.com/workitem/8415
  • 啊,是的,DataGridComboBoxColumn 不是一个实际的选择器,但是每行生成的该列的内容将包含一个ComoBox,它是Selector 的一种类型。所以是的,针对这种特定情况的完整解决方案是确保为生成的ComboBox 设置样式。
  • @GuillaumeA 链接指向通用页面archive.codeplex.com/?p=wpf
猜你喜欢
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多