ComboBox SelectedItem 属性 更新我的 Datagrid 行中的所有其他组合框

ComboBox SelectedItem property updating all other comboboxes in my Datagrid row

我在这个网站上看到了一些与我的问题类似的帖子,比如 this one and this one 但我还没有找到适合我的方法。

我有一个 datagrid 绑定到 Foo 类型的对象列表,我有一个 combobox 为每一行添加。 ComboBox ItemSource 不是 Foo class 的一部分,而是创建于视图模型。我知道这样做意味着这个组合框对于每一行都是相同的,但是在我的 Xaml 中没有办法将 SelectedItem 过滤到该行吗?

这是我的 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>

这是我的视图模型:

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;

我看到我的组合框集合已填充,但是当我进行选择时,每个其他组合框都获得相同的值。谁能帮助我理解我做错了什么?非常感谢。

要使您的代码正常工作,您需要将 SelectedComboBoxItem 绑定到 DataGrid 项目。在您的情况下,这是 Foo 类型

I have a datagrid bound to by a list of objects of type Foo

将此代码放入 Foo class

public string SelectedComboBoxItem
{
    get
    {
        return __SelectedComboBoxItem;
    }
    set
    {
        if (_SelectedComboBoxItem != value)
        {
            _SelectedComboBoxItem = value;
            RaisePropertyChanged(nameof(SelectedComboBoxItem));
        }
    }
}

private string _SelectedComboBoxItem = string.Empty;

并根据

更新您的绑定
SelectedItem="{Binding SelectedComboBoxItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"