如何知道用户编辑 WPF DataGrid 单元格时为空? [英] How to know while user editing the WPF DataGrid Cell is empty?
问题描述
我有一个 WPF
DataGrid
.用户可以编辑cell
中的数据.我想要一个 event
,因为我想检查 cell
是否为 empty
.用户可以使用Del
Backspace
Cut
选项等来清空数据
I have a WPF
DataGrid
. The users can edit the data in cell
. I want a event
in that I want to check if cell
is empty
. User can empty the data by using Del
Backspace
Cut
options etc.
给我一个 event
和 event handler
来做到这一点.我已经尝试过 OnCellEditEnding
event
但这只会在编辑完成时触发.我想在每次用户 inputs
时动态检查 cell
是否为空.
Give me a event
and event handler
to do that. I already tries OnCellEditEnding
event
but this will trigger only if edit is finished. I want to check cell
is empty dynamically each time when user inputs
.
推荐答案
每个数据网格单元在处于编辑模式时都有一个文本框作为其内容.您可以在按键按下时检查该文本框中写入的文本长度(通过处理 onKeyDown 或 onPreviewKeyDown 事件)
each datagridcell has a textbox as its content when it is in the edit mode. you can check the text Length written in that textbox whenever a key is down (by handling onKeyDown or onPreviewKeyDown event)
使用 PreparingCellForEdit 事件,像这样:
use PreparingCellForEdit event, sth like this:
void MainDataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
TextBox tb = e.Column.GetCellContent(e.Row) as TextBox;
tb.TextChanged+=new TextChangedEventHandler(tb_TextChanged);
}
void tb_TextChanged(object sender, TextChangedEventArgs e)
{
//here, something changed the cell's text. you can do what is neccesary
}
这篇关于如何知道用户编辑 WPF DataGrid 单元格时为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!