DataGrid:在单元格验证错误上,其他行单元格是不可编辑/只读

Gaz*_*azi 13 wpf datagrid

在我的wpf数据网格中,我已经实现了验证IDataErrorInfo.当单元格中出现错误时,其他行中的单元格将变为ReadOnly.对我来说这是有道理的,但是企业希望能够在不修复错误的情况下更改其他行单元格,即在某些情况下让用户弄得一团糟,而且开发人员的生活很糟糕.

我已经尝试重置HasCellValidationError为false但它没有解决它.我非常感谢有关此问题的任何反馈/建议.

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

if (inf != null)
{
    inf.SetValue(myDataGrid, false, null);
}
Run Code Online (Sandbox Code Playgroud)

Gaz*_*azi 14

通过覆盖datagrid的OnCanExecuteBeginEdit方法找到解决方案.请参阅下面的代码,目前测试人员没有投诉.

/// <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
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢@Gazi提供最稀有的答案;)。因为我希望网格始终处于可编辑模式,所以我只写了e.CanExecute = true;。e.Handled = true; 在OnCanExecuteBeginEdit方法中。它的运作就像一种魅力。:) (2认同)