2010-11-04 58 views
12

Trong lược đồ dữ liệu wpf của tôi, tôi đã thực hiện xác thực bằng cách sử dụng IDataErrorInfo. Khi có lỗi trong một ô, các ô trong các hàng khác sẽ trở thành ReadOnly. Đối với tôi điều này có ý nghĩa, nhưng doanh nghiệp muốn có thể thay đổi các ô hàng khác mà không sửa lỗi, tức là trong một số tình huống cho phép người dùng tạo ra một mớ hỗn độn và cuộc sống của nhà phát triển kém khốn khổ.DataGrid: Lỗi xác thực ô trên các ô hàng khác không thể chỉnh sửa/Readonly

Tôi đã thử đặt lại HasCellValidationError thành sai nhưng không khắc phục được. Tôi rất đánh giá cao bất kỳ phản hồi/gợi ý nào về vấn đề này.

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

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

Trả lời

13

Tìm thấy giải pháp bằng cách ghi đè phương pháp OnCanExecuteBeginEdit của DataGrid. Xem mã bên dưới và cho đến nay người thử nghiệm không khiếu nại.

/// <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

Cảm ơn @Gazi vì hiếm có câu trả lời hiếm nhất;). Vì tôi muốn lưới của tôi luôn ở chế độ có thể chỉnh sửa, tôi đơn giản viết e.CanExecute = true; e.Handled = true; trong phương thức OnCanExecuteBeginEdit. và nó hoạt động như một sự quyến rũ. :) –

+0

@NareshRavlani Solution by Gazi không hoạt động, nhưng giải pháp của bạn hoạt động như một sự quyến rũ. Bạn đã tìm thấy bất kỳ vấn đề nào với giải pháp của mình sau khi đăng nhận xét này chưa? – Vishal

Các vấn đề liên quan