The DataGridComboBoxColumn
does exactly what you're looking for. To use it correctly, you need to understand the following properties:
SelectedValueBinding
- this is the binding to the property on your object/viewmodelSelectedValuePath
- this is the value property on the items inside theComboBox
. This will be assigned to the property you set inSelectedValueBinding
when the user selects an item from theComboBox
.DisplayMemberPath
- this is the description property on the items inside theComboBox
Setting the ItemsSource
of the DataGridComboBoxColumn
is a little different; note my example below to see how it is done.
These are the same (except for SelectedValueBinding
) as you have on a standard 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>