用户编辑WPF DataGrid Cell时如何知道?

我有一个WPF DataGrid 。 用户可以编辑cell的数据。 我想要一个event ,我想检查cell是否为empty 。 用户可以使用Del Backspace Cut选项等清空数据。

给我一个eventevent handler来做到这一点。 我已经尝试过OnCellEditEnding event但只有在编辑完成后才会触发。 我希望每次用户inputs时检查cell是否为空。

每个datagridcell在处于编辑模式时都有一个文本框作为其内容。 每当一个键关闭时,你可以检查写在该文本框中的文本长度(通过处​​理onKeyDownonPreviewKeyDown事件)

编辑:

使用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 } 

使用数据绑定 :

        

items source是一系列对象,如下所示:

 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 { /* ... */} }