2014-09-26 58 views

回答

4

你可以定制你datagrid并覆盖其OnCanExecuteBeginEdit事件是这样的:

protected override void OnCanExecuteBeginEdit(System.Windows.Input.CanExecuteRoutedEventArgs e) 
    { 
     var hasCellValidationError = false; 
     var hasRowValidationError = false; 
     const BindingFlags bindingFlags = 
      BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance; 
     //Current cell 
     var cellErrorInfo = this.GetType().BaseType.GetProperty("HasCellValidationError", bindingFlags); 
     //Grid row 
     var rowErrorInfo = this.GetType().BaseType.GetProperty("HasRowValidationError", bindingFlags); 
     if (cellErrorInfo != null) hasCellValidationError = (bool) cellErrorInfo.GetValue(this, null); 
     if (rowErrorInfo != null) hasRowValidationError = (bool) rowErrorInfo.GetValue(this, null); 
     base.OnCanExecuteBeginEdit(e); 
     if ((!e.CanExecute && hasCellValidationError) || (!e.CanExecute && hasRowValidationError)) 
     { 
      e.CanExecute = true; 
      e.Handled = true; 
     } 
} 

它检查datagrid有验证错误,然后将其e.CanExecute = true,这样你就可以在键入另一个细胞。

+0

是的,它的作品!但是,UI逻辑仍然是越野车。例如。如果我的datagrid具有'CanUserAddRows =“True”'。步骤如下:在空行中输入无效值以添加新项目,按Enter,转到某个先前存在的行,输入无效值,按Enter,按Escape,在最近添加的行中输入有效值,按Enter。修正先前存在的行中的错误。这样做我会得到所有有效的行并且没有空行来添加新项目。 – 2016-06-05 16:24:31

0

添加CellEditEnding属性的DataGrid

CellEditEnding="DataGridCellEditEnding"
实现该方法

private void DataGridCellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
 
{ 
 
    e.Cancel = true; 
 
}

相关问题