C# WPF MVVM DataGridComboxColumn更改一行更新所有
我看到了一个数据网格。其中一列是DataGridComboxColumn,itemsource通过静态资源绑定到我的视图模型中的列表。问题是,当我更改一行中的选定项时,其他行中的选定项相同 视图模型C# WPF MVVM DataGridComboxColumn更改一行更新所有,c#,wpf,mvvm,C#,Wpf,Mvvm,我看到了一个数据网格。其中一列是DataGridComboxColumn,itemsource通过静态资源绑定到我的视图模型中的列表。问题是,当我更改一行中的选定项时,其他行中的选定项相同 视图模型 public class MyViewModel { private ObservableCollection<Wavelength> wlList = new ObservableCollection<Wavelength>(); private Observa
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; }
}
}
这个问题不是真正的问题;信不信由你,它实际上是特意设计的(这是一个特性,不是一个bug)
我们有财产。如果直接将数据绑定到采集视图
,则它会保持数据同步。从类继承的每个类上都有一个选项:
如果始终与中的当前项同步,则为true错误如果项目从未与当前项目同步null如果仅在使用时与当前项同步。默认值为null
关键部分是它默认为null,它在
CollectionView
s上同步(这就是您要进行数据绑定的对象)。因此,只需在组合框中将属性设置为false,您就可以完成所有设置。SelectedValueBinding=“{Binding Wavelength}”&SelectedValuePath=“Value”是否可以替换为SelectedItemBinding?你能发布AcquisitionParameters类吗?我不明白你是如何绑定到StaticResource wlList的,因为wlList在你的XAMLI中不是可用的资源,我假设你已经从该xaml中剪切了一堆数据?例如,将wavelenghtList缩短为wlList,省略了XAML标记也选择了SelectedValueBinding和SelectedValuePath意味着您将把所选波长的double值绑定到AcquisitionParameters的WaveLength属性,我假设该属性也是double?另外,您确定没有向集合添加相同的Model.AcquisitionParameters对象吗?谢谢!由于DataGridComboxColumn没有IsSynchronizedWithCurrentItem属性,因此解决方案并非真正完整。解决方法是使用样式设置器:啊,是的,datagridcomboxcolumn
不是一个实际的选择器,但是生成的每行该列的内容将包含一个ComoBox
,它是一种选择器。因此,是的,针对这种特定情况的完整解决方案是确保为生成的链接指向通用页面的组合框
@GuillaumeA设置样式。
<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);
}
}