Hello. I have a DataGrid with a ComboBox DataTemplate column. The ComboBox is editable. The problem is that when I mouse-click into the editable portion of a ComboBox in a different row, the DataGrid's SelectionChanged event does not fire. It does when I click on the down arrow of the combobox, just not when I click into the text edit area. I need to be able to update the bound DataRowView with the ComboBox information but to do this I need a reference to the correct SelectedItem in the DataGrid. Any help would be appreciated. Here's my XAML for the ComboBox column:
<DataGridTemplateColumn Header="Name"><DataGridTemplateColumn.CellTemplate><DataTemplate><ComboBox IsEditable="True" SelectedValue="{Binding Path=IngredientID}" SelectedValuePath="ID" DisplayMemberPath="Name" Loaded="NameCol_Loaded" SelectionChanged="NameCol_SelectionChanged" KeyUp="ComboBox_KeyUp" /></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>
Here's the code where I populate the ComboBox in each row:
Private Sub UnitsCol_Loaded(sender As Object, e As RoutedEventArgs) CType(sender, ComboBox).ItemsSource = _dtUnit.DefaultView End Sub
And here's when I try to access the SelectedItem of the DataGrid, which returns Nothing if, after the grid is populated, the first thing I do is to click inside the text portion of the ComboBox:
Private Sub ComboBox_KeyUp(sender As Object, e As KeyEventArgs) Dim cbo As ComboBox = CType(sender, ComboBox) Dim r As DataRowView = DirectCast(dgRecipeIngredients.SelectedItem, DataRowView) If r IsNot Nothing Then r.Item("IngredientName") = cbo.Text End If End Sub
Here's what I'm using when the user just clicks the down arrow of the ComboBox and selects an entry and this does seem to work ok:
Private Sub NameCol_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Dim r As DataRowView = CType(dgRecipeIngredients.SelectedItem, DataRowView) If r IsNot Nothing Then Dim cbo As ComboBox = CType(sender, ComboBox) r.Item("IngredientID") = cbo.SelectedValue End If End Sub
Thanks!