【发布时间】: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 对象添加到集合中吗?