Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何知道用户编辑WPF DataGrid单元格时是否为空?_C#_Wpf_Datagrid - Fatal编程技术网

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命令?