I have DataGrid and its itemsource is a list of object which is named WeightItemData.
My all columns in data grid are DataGridTextColumn except one that is DataGridTemplateColumn and it is a Combobox.
My problem is that i cannot get the updated combox value inside the DataGridCellEditEnding method.
<DataGridTemplateColumn Header="Crew" SortMemberPath="FsmTypes">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding FsmType}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding FsmTypes, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding FsmType,Mode=TwoWay}"></ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
private void DataGridCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
TextBlock cmb = e.EditingElement as TextBlock;
if (e.EditAction == DataGridEditAction.Commit)
{
WeightItemData wid = e.Row.DataContext as WeightItemData;
}
}
I just solved my problem as below.
private void DataGridCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
WeightItemData wid = e.Row.DataContext as WeightItemData;
if (e.Column.SortMemberPath.Equals("FsmTypes")) {
FrameworkElement elmtTest = e.Column.GetCellContent(e.Row);
ComboBox fsmTypeCombo = ApplicationUtility.FindVisualChild<ComboBox>(elmtTest);
if(fsmTypeCombo!=null)
{
wid.FsmType = fsmTypeCombo.SelectedValue.ToString();
}
}
}
}
public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}