Prodromos
Prodromos

Reputation: 146

WPF DataGrid Combobox Binding

I tried changing DataGridComboBoxColumn to DataGridTemplateColumn without success.

The DataGridComboBoxColumn is working as expected but the Combobox in DataGridTemplateColumn is not. If I change a value in this Combobox, it will change all the visible Comboboxes values in all visible Rows.

What I'm I missing ?

DataGrid is like this:

<DataGrid x:Name="bookDataGrid"
                      AutoGenerateColumns="False"
                      EnableRowVirtualization="True"
                      ItemsSource="{Binding Source={StaticResource bookViewSource}}">

The DataGridComboboxColumn like this:

                    <DataGridComboBoxColumn x:Name="countryColumn"
                                        ItemsSource="{Binding Source={StaticResource countryLookup}}"
                                        DisplayMemberPath="CountryName"
                                        SelectedValuePath="ID"
                                        SelectedValueBinding="{Binding Country,UpdateSourceTrigger=PropertyChanged}"
                                        Header="Country"
                                        Width="SizeToCells" />

It is used to set the Country (ID) in Books Table. I use CollectionViewSource for Books (bookViewSource) and Country (countryLookup).

The not working DataGridTemplateColumn like this:

                    <DataGridTemplateColumn x:Name="CountryTemplateColumn">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <ComboBox x:Name="CountryCombo"                                              
                                          ItemsSource="{Binding Source={StaticResource countryLookup}}"
                                          DisplayMemberPath="CountryName"
                                          SelectedValuePath="ID"
                                          SelectedValue="{Binding Country, Source={StaticResource bookViewSource}, UpdateSourceTrigger=PropertyChanged}">
                                </ComboBox>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

Please Help. Thanks.

Upvotes: 0

Views: 1063

Answers (2)

Prodromos
Prodromos

Reputation: 146

This solved my Problem:

Add IsSynchronizedWithCurrentItem="False" to the Combobox in the DataGridTemplateColumn and remove the Source={StaticResource bookViewSource} as mm8 suggested.

Upvotes: 1

mm8
mm8

Reputation: 169390

Remove Source={StaticResource bookViewSource}:

<ComboBox x:Name="CountryCombo"                                              
                ItemsSource="{Binding Source={StaticResource countryLookup}}"
                DisplayMemberPath="CountryName"
                SelectedValuePath="ID"
                SelectedValue="{Binding Country, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>

Upvotes: 0

Related Questions