How to bind (Itemssource and selected item) of a combobox inside wpf datagrid? I am using MVVM pattern. I have tried
<DataGridTemplateColumn Header="Example 9">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding PartIds, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
and
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding MyCars}" HorizontalAlignment="Left">
<DataGrid.Columns>
<DataGridTextColumn Header="Model" Binding="{Binding Model}"/>
<DataGridTextColumn Header="Registration" Binding="{Binding Registration}"/>
<DataGridTemplateColumn Header="Example 12">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding CarParts, RelativeSource={RelativeSource AncestorType=Window}}" DisplayMemberPath="PartName" SelectedValuePath="PartID" SelectedValue="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Selected" Binding="{Binding PartId}"/>
</DataGrid.Columns>
</DataGrid>
Properties used for data-binding
#region DataGrid List<String> Example
public ObservableCollection<MyCar> MyCars { get; set; }
public List<string> PartIds { get; set; }
#endregion
#region DataGrid List<Class> Example
public List<CarPart> CarParts { get; set; }
#endregion
Reference: http://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82