WPF DataGrid 组合框绑定

WPF DataGrid Combobox Binding

我尝试将 DataGridComboBoxColumn 更改为 DataGridTemplateColumn 但没有成功。

DataGridComboBoxColumn 按预期工作,但 DataGridTemplateColumn 中的组合框没有。如果我更改此组合框中的值,它将更改所有 visible 行中的所有 visible 组合框值。

我错过了什么?

数据网格是这样的:

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

DataGridComboboxColumn是这样的:

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

用于设置图书中的国家(ID)Table。我对图书 (bookViewSource) 和国家 (countryLookup) 使用 CollectionViewSource

不工作 DataGridTemplateColumn 像这样:

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

请帮忙。谢谢。

删除Source={StaticResource bookViewSource}:

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

这解决了我的问题:

将 IsSynchronizedWithCurrentItem="False" 添加到 DataGridTemplateColumn 中的 Combobox 并按照 mm8 的建议删除 Source={StaticResource bookViewSource}。