如何在 DataGrid CellEditEnding 方法中获取 DataGridTemplateColumn 的值
How to get value of DataGridTemplateColumn in DataGridCellEditEnding method
我有 DataGrid,它的项目源是一个名为 WeightItemData 的对象列表。
我在数据网格中的所有列都是 DataGridTextColumn,除了一个是 DataGridTemplateColumn 并且它是一个 Combobox。
我的问题是我无法在 DataGridCellEditEnding 方法中获取更新后的组合框值。
<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;
}
}
我刚刚解决了下面的问题。
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;
}
我有 DataGrid,它的项目源是一个名为 WeightItemData 的对象列表。
我在数据网格中的所有列都是 DataGridTextColumn,除了一个是 DataGridTemplateColumn 并且它是一个 Combobox。
我的问题是我无法在 DataGridCellEditEnding 方法中获取更新后的组合框值。
<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;
}
}
我刚刚解决了下面的问题。
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;
}