user2529011
user2529011

Reputation: 733

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.

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?

Here is my 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>

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.

Upvotes: 0

Views: 595

Answers (1)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23228

To make your code working you'll need to bind the SelectedComboBoxItem to DataGrid item. In your case this is a Foo type

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

Place this code to Foo class

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}"

Upvotes: 1

Related Questions