I can add a Combobox to a DataGrid using following xmal:
<local:DataGridTemplateColumn Header="SomeHeader" Width="106" HeaderStyle="{StaticResource headerAlignRightStyle}" CellStyle="{StaticResource cellAlignRightStyle}">
<local:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SomeProp}" Margin="4"/>
</DataTemplate>
</local:DataGridTemplateColumn.CellTemplate>
<local:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
x:Name="SomeCombo"
SelectionChanged="SomeCombo_SelectionChanged"
ItemsSource="{Binding SomeList}"
DisplayMemberPath="Name"
/>
</DataTemplate>
</local:DataGridTemplateColumn.CellEditingTemplate>
</local:DataGridTemplateColumn>
However what I can’t figure out is a sensible way to get the row that was combox is
bound to. i.e. when handling the combobox SelectionChanged event I have no way of knowing what
what row the combobox belongs to. Particularly I don’t know what object in the DataGrid datasource
that the combobox is refering to.
Any help would be much appreciated.
you could
A) Bind the SelectedItem property of the ComboBox to a property in your ViewModel/data model using a two way binding, so you wouldn’t have to worry about SelectionChanged in the first place
or
B) Use DataGridRow.GetRowContainingElement(element) in your SelectionChanged handler, i.e.
Cheers, Alex