2010-11-04 8 views
12

En mi cuadrícula de datos wpf he implementado la validación usando IDataErrorInfo. Cuando hay un error en una celda, las celdas en otras filas se convierten en ReadOnly. Para mí, esto tiene sentido, pero las empresas quieren poder cambiar otras celdas de fila sin corregir el error, es decir, en algunos casos los usuarios pueden hacer un lío y la vida de un desarrollador pobre es miserable.DataGrid: en el error de validación de celda, otras celdas de fila no pueden editarse/Readonly

He intentado restablecer HasCellValidationError a falso, pero no lo solucionó. Agradeceré cualquier comentario o sugerencia sobre este tema.

BindingFlags bf = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance; 
PropertyInfo inf = myDataGrid.GetType().GetProperty("HasCellValidationError", bf); 

if (inf != null) 
{ 
    inf.SetValue(myDataGrid, false, null); 
} 

Respuesta

13

Encontré una solución anulando el método OnCanExecuteBeginEdit de la cuadrícula de datos. Vea el código a continuación y hasta ahora los evaluadores no tienen quejas.

/// <summary> 
/// This class overrides the OnCanExecuteBeginEdit method of the standard grid 
/// </summary> 
public partial class DataGrid : System.Windows.Controls.DataGrid 
{ 

    /// <summary> 
    /// This method overrides the 
    /// if (canExecute && HasRowValidationError) condition of the base method to allow 
    /// ----entering edit mode when there is a pending validation error 
    /// ---editing of other rows 
    /// </summary> 
    /// <param name="e"></param> 
    protected override void OnCanExecuteBeginEdit(System.Windows.Input.CanExecuteRoutedEventArgs e) 
    { 

     bool hasCellValidationError = false; 
     bool hasRowValidationError = false; 
     BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance; 
     //Current cell 
     PropertyInfo cellErrorInfo = this.GetType().BaseType.GetProperty("HasCellValidationError", bindingFlags); 
     //Grid level 
     PropertyInfo 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 && hasRowValidationError) 
     { 
      e.CanExecute = true; 
      e.Handled = true; 
     } 
    } 

    #region baseOnCanExecuteBeginEdit 
    //protected virtual void OnCanExecuteBeginEdit(CanExecuteRoutedEventArgs e) 
    //{ 
    // bool canExecute = !IsReadOnly && (CurrentCellContainer != null) && !IsEditingCurrentCell && !IsCurrentCellReadOnly && !HasCellValidationError; 

    // if (canExecute && HasRowValidationError) 
    // { 
    //  DataGridCell cellContainer = GetEventCellOrCurrentCell(e); 
    //  if (cellContainer != null) 
    //  { 
    //   object rowItem = cellContainer.RowDataItem; 

    //   // When there is a validation error, only allow editing on that row 
    //   canExecute = IsAddingOrEditingRowItem(rowItem); 
    //  } 
    //  else 
    //  { 
    //   // Don't allow entering edit mode when there is a pending validation error 
    //   canExecute = false; 
    //  } 
    // } 

    // e.CanExecute = canExecute; 
    // e.Handled = true; 
    //} 
    #endregion baseOnCanExecuteBeginEdit 
} 
+1

Gracias @Gazi por raro de la respuesta más rara;). Como quiero que mi grilla esté siempre en modo editable, simplemente escribí e.CanExecute = true; e.Handled = verdadero; en el método OnCanExecuteBeginEdit. y está funcionando como un encanto. :) –

+0

@NareshRavlani Solución de Gazi no funcionó, pero su solución funciona como un encanto. ¿Has encontrado algún problema con tu solución después de publicar este comentario? – Vishal

Cuestiones relacionadas