wpf 绑定不同类型的DataGrid中ComboBox的SelectedItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16697121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:50:56  来源:igfitidea点击:

Binding SelectedItem of ComboBox in DataGrid with different type

wpfbindingdatagridcomboboxdatagridcomboboxcolumn

提问by Maxcom

I have a DataGrid which contains ComboBox as column.

我有一个包含 ComboBox 作为列的 DataGrid。

Let consider DataGrid having ItemsSource as ObservableCollection and ComboBox ItemsSource is List .

让我们考虑 DataGrid 的 ItemsSource 作为 ObservableCollection 和 ComboBox ItemsSource 是 List 。

I want to set the ComboBox SelectedItem property based on property in DataGrid ItemsSource.

我想根据 DataGrid ItemsSource 中的属性设置 ComboBox SelectedItem 属性。

However Product class has property ProductTypeId of type int and not ProductType.

但是,Product 类具有 int 类型而不是 ProductType 的属性 ProductTypeId。

So how can I set ComboBox SelectedItem so that it display value of Product.ProductTypeId as selected. And also I want to bind SeletedItems with Mode = TwoWay so that whenever ComboBox SelectedItem changes it will be reflected in DataGrid's ItemsSource.

那么我如何设置 ComboBox SelectedItem 以便它显示 Product.ProductTypeId 的值作为选择。而且我想将 SeletedItems 与 Mode = TwoWay 绑定,这样每当 ComboBox SelectedItem 更改时,它将反映在 DataGrid 的 ItemsSource 中。

Any help would be much appreciated.

任何帮助将非常感激。

Thanks.

谢谢。

回答by Brian S

The DataGridComboBoxColumndoes exactly what you're looking for. To use it correctly, you need to understand the following properties:

DataGridComboBoxColumn正是你正在寻找的。要正确使用它,您需要了解以下属性:

  • SelectedValueBinding- this is the binding to the property on your object/viewmodel
  • SelectedValuePath- this is the value property on the items inside the ComboBox. This will be assigned to the property you set in SelectedValueBindingwhen the user selects an item from the ComboBox.
  • DisplayMemberPath- this is the description property on the items inside the ComboBox
  • SelectedValueBinding- 这是对您的对象/视图模型上的属性的绑定
  • SelectedValuePath- 这是ComboBox. 这将被分配给您在设置属性SelectedValueBinding,当用户从选择一个项目ComboBox
  • DisplayMemberPath- 这是内部项目的描述属性 ComboBox

Setting the ItemsSourceof the DataGridComboBoxColumnis a little different; note my example below to see how it is done.

设置ItemsSourceDataGridComboBoxColumn是一个有点不同; 请注意我下面的示例以了解它是如何完成的。

These are the same (except for SelectedValueBinding) as you have on a standard ComboBox.

这些SelectedValueBinding与您在标准ComboBox.

Here is an example of what your column might look like.

以下是您的列可能是什么样子的示例。

<DataGridComboBoxColumn Header="Product Type" DisplayMemberPath="ProductType" SelectedValuePath="ProductTypeId" SelectedValueBinding="{Binding ProductTypeId, UpdateSourceTrigger=PropertyChanged}">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="ItemsSource" Value="{Binding AvailableProductTypes}"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="ItemsSource" Value="{Binding AvailableProductTypes}"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>