【问题标题】:WPF DataGrid Combobox BindingWPF DataGrid 组合框绑定
【发布时间】:2018-01-17 01:44:42
【问题描述】:

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

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

我错过了什么?

DataGrid 是这样的:

<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)。我将 CollectionViewSource 用于 Books (bookViewSource) 和 Country (countryLookup)。

不工作的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>

请帮忙。谢谢。

【问题讨论】:

    标签: wpf data-binding combobox datagrid wpfdatagrid


    【解决方案1】:

    这解决了我的问题:

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

    【讨论】:

      【解决方案2】:

      删除Source={StaticResource bookViewSource}

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

      【讨论】:

      • 已删除但...我遇到了其他问题:它也在更改可见行的数据库。也就是说:如果我为第一行选择一个国家,它将更改数据库中可见行的国家!
      • 您的 Book 实体类应该有一个 Country 属性,该属性保存图书当前选定属性的 id。当您在数据库中选择一个项目时,会设置此属性。
      • 是的,这是正确的。它有一个 Country 属性,即从 Combobox 中选择的 ID。奇怪的想法是 DataGridComboboxColumn 工作正常。但是 Tamplatecomumn 不是!
      • 应该的。尝试将 ComboBox 放入 .
      • 我只是将 DataGridComboBoxColumn 复制到 tamplate 列。我将 DataGridComboBoxColumn 更改为 Combobox,将 SelectedValueBinding 更改为 SelectedValue。没有成功同样的问题。如果我添加 Source={StaticResource bookViewSource} 则数据库值不会更改,只有可视组合框值(7 行)。如果让您感到困惑,我很抱歉!
      猜你喜欢
      • 2016-05-02
      • 2016-12-10
      • 2022-10-07
      • 2023-03-18
      • 1970-01-01
      • 2012-06-18
      • 2011-03-09
      相关资源
      最近更新 更多