DataGrid.BeginningEdit event not firing when I use TextBox in DataGridTemplateColumn.CellTemplate

可紊 提交于 2020-07-09 08:38:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!