DataGridTemplateColumn的RowEditEnding事件无效

时间:2015-11-27 09:58:36

标签: c# wpf event-handling wpfdatagrid

我有以下wpf网格我想在用户更改事件不适用于DataGridTemplateColumn时调用RowEditEnding事件。  仅当我更改DataGridTemplateColumn定义的控件(例如datagridDataGridTextColumn

中的值时,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事件。

1 个答案:

答案 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>