user1841243
user1841243

Reputation: 1713

xaml: multiple selected items in datagrid, each datagrid has a combobox, selecteditem changes every combobox - should only change selected combobox

I have a datagrid like so:

  <DataGrid ItemsSource="{Binding Path=MyItems, Mode=OneWay}"
      SelectedItem="{Binding SelectedItem, Mode=TwoWay}">

in each item of the datagrid I have a combobox with a list of items:

<ComboBox ItemsSource="{Binding SelectedItem.SubItems}"
      SelectedItem="{Binding SelectedComboItem}"
      IsSynchronizedWithCurrentItem="True"/>

unfortunately, whenever I change 1 combobox, all other comboboxes also change their selected item.

Help ! I've looking for a solution for an entire day now...

Upvotes: 0

Views: 432

Answers (1)

Mashton
Mashton

Reputation: 6415

Here's a quick example from something in use here, I've chopped out everything apart from the combobox within a datagrid so hopefully you can see where you've gone wrong with your bindings/logic:

<sdk:DataGrid x:Name="MyDatagrid" AutoGenerateColumns="False" ItemsSource="{Binding MyItems}">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn Width="Auto" Header="Combo column"  IsReadOnly="False">
            <sdk:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding SubItems, Mode=OneWay}" SelectedItem="{Binding SelectedComboItem,Mode=TwoWay}" />
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellEditingTemplate>
        </sdk:DataGridTemplateColumn>
    </sdk:DataGrid.Columns>
</sdk:DataGrid> 

Things to note: MyItems is a ViewModel property SubItems is a property of class of items within MyItems, not the ViewModel. If you want your combobox ItemSource to come from the viewmodel then you will need something like:

{Binding DataContext.AllComboItems,  RelativeSource={RelativeSource AncestorType=sdk:DataGrid}, Mode=OneWay}

SelectedComboItem is also a property on the class of items within MyItems, not the ViewModel.

Upvotes: 1

Related Questions