ComboBox SelectedItem属性更新了我的Datagrid行中的所有其他组合框 [英] ComboBox SelectedItem property updating all other comboboxes in my Datagrid row
问题描述
我在该网站上看到了几则与我的问题类似的帖子,例如这一篇和这一个但是我还没有为我工作.
I have seen a couple of posts in this site similar to my issue like this one and this one But I haven't gotten this to work for me.
我有一个 datagrid ,由 Foo 类型的对象列表绑定,并且为每行添加了 combobox . ComboBox ItemSource 不是 Foo 类的一部分,而是在视图模型中创建的.我知道这样做意味着此组合框的每一行都是相同的,但是在我的Xaml中没有办法将SelectedItem过滤到仅该行吗?
I have a datagrid bound to by a list of objects of type Foo and I have a combobox added for each row. The ComboBox ItemSource is not a part of the Foo class but rather it's created in the view model. I know doing so means that this combobox is the same for every row but isn't there a way in my Xaml to filter SelectedItem to just the row?
这是我的Xaml:
<DataGridTemplateColumn Header="Foo Column" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding Mode=OneWay,Path=DataContext.FooCollection,
RelativeSource= {RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}}}"
SelectedItem="{Binding DataContext.SelectedComboBoxItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
RelativeSource= {RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}}}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
这是我的ViewModel:
Here is my ViewModel:
public ObservableCollection<string> FooCollection
{
get
{
return _FooCollection;
}
set
{
if (_FooCollection != value)
{
_FooCollection = value;
RaisePropertyChanged(nameof(FooCollection));
}
}
}
private ObservableCollection<string> _FooCollection = new ObservableCollection<string>();
public string SelectedComboBoxItem
{
get
{
return __SelectedComboBoxItem;
}
set
{
if (_SelectedComboBoxItem != value)
{
_SelectedComboBoxItem = value;
RaisePropertyChanged(nameof(SelectedComboBoxItem));
}
}
}
private string _SelectedComboBoxItem = string.Empty;
我看到组合框集合已填充,但是当我进行选择时,其他所有组合框都具有相同的值.谁能帮助我了解我做错了什么?非常感谢.
I am seeing my combobox collection populated but when I make a selection every other combobox gets the same value. Can anyone help me understand what I am doing wrong? Many thanks.
推荐答案
要使代码正常工作,您需要将 SelectedComboBoxItem
绑定到 DataGrid
项.在您的情况下,这是 Foo
类型
To make your code working you'll need to bind the SelectedComboBoxItem
to DataGrid
item. In your case this is a Foo
type
我有一个由Foo类型的对象列表绑定的数据网格
I have a datagrid bound to by a list of objects of type Foo
将此代码放入 Foo
类
public string SelectedComboBoxItem
{
get
{
return __SelectedComboBoxItem;
}
set
{
if (_SelectedComboBoxItem != value)
{
_SelectedComboBoxItem = value;
RaisePropertyChanged(nameof(SelectedComboBoxItem));
}
}
}
private string _SelectedComboBoxItem = string.Empty;
并根据该信息更新您的绑定
and update your binding according that
SelectedItem="{Binding SelectedComboBoxItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
这篇关于ComboBox SelectedItem属性更新了我的Datagrid行中的所有其他组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!