我有以下wpf网格我想在用户更改事件不适用于DataGridTemplateColumn
时调用RowEditEnding事件。
仅当我更改DataGridTemplateColumn
定义的控件(例如datagrid
,DataGridTextColumn
等
DataGridComboBoxColumn
才会执行
<DataGrid Name="DriversDataGrid" Width="360" ItemsSource="{Binding GetAll}" CommandManager.PreviewExecuted="DriversDataGrid_PreviewDeleteCommandHandler" AutoGenerateColumns="False" RowEditEnding="DataGrid_RowEditEnding">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Latest Victory Date" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding LatestVictory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0></DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn >
</DataGrid.Columns>
</DataGrid>
这是我的事件代码
private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
FormulaOneDriver driver = e.Row.DataContext as FormulaOneDriver;
MessageBox.Show("test");
driver.Save();
}
}
如何为RowEditEnding
元素中的更改数据调用DataGridTemplateColumn
事件。
答案 0 :(得分:1)
添加CellEditingTemplate。这需要用户单击两次 - 一次选择单元格,然后再次进入CellEditing模式
<DataGridTemplateColumn Header="Latest Victory Date" SortMemberPath="LatestVictory">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LatestVictory, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding LatestVictory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ConverterCulture='en-GB', StringFormat=d}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>