C# 如何知道用户编辑WPF DataGrid单元格时是否为空?
我有一个C# 如何知道用户编辑WPF DataGrid单元格时是否为空?,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个WPFDataGrid。用户可以在单元格中编辑数据。我想要一个事件,我想检查单元格是否为空。用户可以使用DelBackspaceCut选项等清空数据 请给我一个事件和事件处理程序来执行此操作。我已经尝试了oncelletending事件,但只有编辑完成后才会触发。我想在每次用户输入时动态检查单元格是否为空。在编辑模式下,每个datagridcell都有一个文本框作为其内容。每当按键按下时,您可以检查写入该文本框的文本长度(通过处理onKeyDown或onPreviewKeyDown事件)
WPF
DataGrid
。用户可以在单元格
中编辑数据。我想要一个事件
,我想检查单元格
是否为空
。用户可以使用Del
Backspace
Cut
选项等清空数据
请给我一个
事件
和事件处理程序
来执行此操作。我已经尝试了oncelletending
事件
,但只有编辑完成后才会触发。我想在每次用户输入时动态检查单元格
是否为空。在编辑模式下,每个datagridcell都有一个文本框作为其内容。每当按键按下时,您可以检查写入该文本框的文本长度(通过处理onKeyDown或onPreviewKeyDown事件)
编辑:
使用PreparingCellForEdit事件,类似以下内容:
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
}
public class Customer : INotifyPropertyChanged
{
public string FirstName
{
get { return firstName; }
set
{
if (string.IsNullOrEmpty(value))
{
// oops!
}
if (firstName != value)
{
firstName = value;
OnPropertyChanged("FirstName"); // raises INotifyPropertyChanged.PropertyChanged
}
}
}
private string firstName;
public string LastName { /* ... */}
}
在编辑模式下,每个datagridcell都有一个文本框作为其内容。每当按键按下时,您可以检查写入该文本框的文本长度(通过处理onKeyDown或onPreviewKeyDown事件)
编辑:
使用PreparingCellForEdit事件,类似以下内容:
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
}
public class Customer : INotifyPropertyChanged
{
public string FirstName
{
get { return firstName; }
set
{
if (string.IsNullOrEmpty(value))
{
// oops!
}
if (firstName != value)
{
firstName = value;
OnPropertyChanged("FirstName"); // raises INotifyPropertyChanged.PropertyChanged
}
}
}
private string firstName;
public string LastName { /* ... */}
}
使用:
使用:
这是错误的假设,请参见DataGridTemplateColumn
。您是对的,但我认为WPFK正在处理DataGridTextColumn。伙计们!如果我在编辑模式下打开并剪切所有数据会怎么样?如何触发OS cut命令?这是错误的假设,请参见DataGridTemplateColumn
。你是对的,但我认为WPFK正在处理DataGridTextColumn。伙计们!如果我在编辑模式下打开并剪切所有数据会怎么样?如何触发OS cut命令?