DataGrid:在单元格验证错误时,其他行单元格是不可编辑的/ Readonly [英] DataGrid: On cell validation error other row cells are uneditable/Readonly

查看:151
本文介绍了DataGrid:在单元格验证错误时,其他行单元格是不可编辑的/ Readonly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的wpf datagrid中,我使用 IDataErrorInfo 实现验证。当单元格中有错误时,其他行中的单元格将变为ReadOnly。对我来说这是有道理的,但企业希望能够改变其他行单元格,而不会修复错误,即在某些情况下,让用户弄乱,糟糕的开发人员的生活凄惨。

In my wpf datagrid I have implemented validation using IDataErrorInfo. When there is a error in a cell, cells in other rows become ReadOnly. To me this makes sense, but business wants to be able to change other row cells without fixing the error i.e. in some scenarios let users make a mess and, poor developer's life miserable.

我尝试将 HasCellValidationError 重置为false,但没有修复它。我非常感谢您对此问题的任何反馈/建议。

I have tried resetting HasCellValidationError to false but it did not fix it. I'll very highly appreciate any feedback/suggestion on this issue.

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

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


推荐答案

通过覆盖OnCanExecuteBeginEdit方法的datagrid。
请看下面的代码,到目前为止,测试人员没有投诉。

Found a solution by overriding the OnCanExecuteBeginEdit method of the datagrid. See the code below and so far testers have not complaint.

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

这篇关于DataGrid:在单元格验证错误时,其他行单元格是不可编辑的/ Readonly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆