当我在DataGridTemplateColumn.CellTemplate中使用TextBox时,不会触发DataGrid.BeginningEdit事件 [英] DataGrid.BeginningEdit event not firing when I use TextBox in DataGridTemplateColumn.CellTemplate

查看:139
本文介绍了当我在DataGridTemplateColumn.CellTemplate中使用TextBox时,不会触发DataGrid.BeginningEdit事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据网格中显示数据。只能编辑一列中的某些单元格。因此,为此,我为一个列定义了Column模板,如下所示:

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>

因此,根据模型对象的只读属性,单元格是否可编辑。效果很好。但是现在我想在用户开始编辑单元格时执行一些操作,因此我为DataGrid创建了BeginningEdit事件的处理程序。但是该事件处理程序没有被调用。我用DataGridCell替换了TextBox。事件处理程序被调用,但我无法编辑单元格值。因此,如何解决此问题。

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.

推荐答案

当单元格进入编辑模式时,即在发生 BeginningEdit 事件时,将应用单元格编辑模板,因此您应该添加您的 TextBox 对此:

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>

这篇关于当我在DataGridTemplateColumn.CellTemplate中使用TextBox时,不会触发DataGrid.BeginningEdit事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆