Reputation: 835
I have a WPF Datagrid, with a datatemplate column for combobox
<DataGrid Name="grdTest" ItemsSource="{Binding Path=TestsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridTemplateColumn Width="120">
<DataGridTemplateColumn.Header >
<TextBlock Text="Test Name" ToolTip="Test Name" Width="109"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="cbxTest"
SelectedValue="{Binding TestID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Test" SelectedValuePath="TestID" SelectionChanged="cbxTest_SelectionChanged_1"
ItemsSource ="{Binding Path=TestList}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I'm trying to get the combobox element in the CellEditEnding event of the datagrid as below:
FrameworkElement elmtTest = grdTest.Columns[7].GetCellContent(e.Row);
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(elmtTest);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
ComboBox myComboBox = (ComboBox)myDataTemplate.FindName("cbxTest", myContentPresenter);
elmtTest is of type System.Windows.Controls.ContentPresenter. But the content template gives null. How can I get the combobox element of the datagrid column?
Upvotes: 1
Views: 3143
Reputation: 835
I got it to work. It was a dumb mistake from my end. I had to pass combobox to FindVisualChild.
FrameworkElement elmtTest = grdTest.Columns[7].GetCellContent(e.Row);
ComboBox myComboBox = FindVisualChild<ComboBox>(elmtTest);
Upvotes: 1