问题
I want to show data in data grid. And only some of the cells in a column can be edited.So, for this purpose I defined the Column template for one column as shown below:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox IsReadOnly="{Binding IsReadOnly}" BorderThickness="0" Text="{Binding Value, UpdateSourceTrigger= LostFocus}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
So, depending upon the read only property of the model object, cell will be editable or not.This is working great.But now I want to perform some operation when user starts editing the cell, so I created a handler for the BeginningEdit event for the DataGrid.But the event handler is not getting called.I replaced the TextBox with DataGridCell.Now, the event handler is called, but I can't edit the cell value.So, how do I solve this issue.
回答1:
It is the CellEditingTemplate that is applied when the cell is put into edit mode, which is when the BeginningEdit
event occurs, so you should add your TextBox
to this one:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox IsReadOnly="{Binding IsReadOnly}" BorderThickness="0"
Text="{Binding Value, UpdateSourceTrigger= LostFocus}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
来源:https://stackoverflow.com/questions/47350096/datagrid-beginningedit-event-not-firing-when-i-use-textbox-in-datagridtemplateco