【问题标题】:DataGrid: On cell validation error other row cells are uneditable/ReadonlyDataGrid:在单元格验证错误时,其他行单元格不可编辑/只读
【发布时间】:2010-11-04 01:06:45
【问题描述】:

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

我尝试将 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);
}

【问题讨论】:

    标签: wpf datagrid


    【解决方案1】:

    通过覆盖数据网格的 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
    }
    

    【讨论】:

    • 感谢@Gazi 提供最稀有的答案;)。因为我希望我的网格始终处于可编辑模式,所以我只写了 e.CanExecute = true; e.Handled = true;在 OnCanExecuteBeginEdit 方法中。它就像一个魅力。 :)
    • @NareshRavlani Gazi 的解决方案不起作用,但您的解决方案就像一个魅力。发表此评论后,您是否发现您的解决方案有任何问题?
    【解决方案2】:

    Gazi's answer 在保持网格可编辑方面起到了作用。但是,虽然通过此实现,用户可以继续编辑其他字段,但这些额外更改的值不会写入您的模型,也无法进行进一步的验证。 当进一步的用户更改会导致额外的验证错误时,问题就会变得很明显。这些不会被引发,因为不会发生源更新和验证。

    要克服这个问题,需要在 OnExecutedCommitEdit 内完成与您类似的实现。见the answer to this multiple DataGrid row validation question

    【讨论】:

      猜你喜欢
      • 2011-04-12
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      相关资源
      最近更新 更多