如何编辑 wpfdatagrid 其他单元格而其中一个单元格无效? [英] How to edit wpfdatagrid other cells while one of it's cell is invalid?

查看:35
本文介绍了如何编辑 wpfdatagrid 其他单元格而其中一个单元格无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF datagrid中,当一个单元格无效时,它会阻止其他单元格编辑,因此用户在无效单元格生效之前无法输入数据.我想知道是否有办法禁用这种行为?

In WPF datagrid,When a cell is invalid,it prevents the the other cells editing so user can not enter data until the invalid cell comes valid.I was wonder if there is a way to disable this behavior?

我是如何使用datagrid的:

<DataGrid ItemsSource="{Binding ..}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name"
         Binding="{Binding Name
         , UpdateSourceTrigger=PropertyChanged
         , NotifyOnValidationError=True
         , ValidatesOnDataErrors=True
         , ValidatesOnExceptions=True}"
    </DataGridTextColumn>
  </DataGrid.Columns>
</DataGrid>

推荐答案

我像下面这样覆盖了 datagrid 的 OnCanExecuteBeginEdit 方法,现在它可以工作了.

I overrided the OnCanExecuteBeginEdit method of the datagrid like below and it works now.

  protected override void OnCanExecuteBeginEdit(System.Windows.Input.CanExecuteRoutedEventArgs e)
    {
        var hasCellValidationError = false;
        var hasRowValidationError = false;
        const BindingFlags bindingFlags =
            BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance;
        var cellError= this.GetType().BaseType.GetProperty("HasCellValidationError", bindingFlags);
        var rowError = this.GetType().BaseType.GetProperty("HasRowValidationError", bindingFlags);

        if (cellError != null) 
            hasCellValidationError = (bool) cellErrorInfo.GetValue(this, null);
        if (rowError != null)
            hasRowValidationError = (bool) rowErrorInfo.GetValue(this, null);

        base.OnCanExecuteBeginEdit(e);
        if ((!e.CanExecute && hasCellValidationError) || (!e.CanExecute && hasRowValidationError))
        {
            e.CanExecute = true;
            e.Handled = true;
        }
    }

同样的问题:DataGrid:开启单元格验证错误其他行单元格不可编辑/只读

这篇关于如何编辑 wpfdatagrid 其他单元格而其中一个单元格无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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