【问题标题】:How to edit wpfdatagrid other cells while one of it's cell is invalid?如何在其中一个单元格无效时编辑 wpf datagrid 其他单元格?
【发布时间】:2013-09-19 12:57:34
【问题描述】:

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

还有我如何使用datagrid

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

【问题讨论】:

    标签: wpf validation xaml wpfdatagrid


    【解决方案1】:

    如果它是一个 mvvm 应用程序并且此行为将在您的应用程序中重复多次, 您可以创建自己的 DataGrid 继承自 DataGrid,并像这样覆盖 OnCellEditEnding 方法:

    public class myDataGrid : DataGrid
    {
        protected override void OnCellEditEnding(DataGridCellEditEndingEventArgs e)
        {
            e.Cancel = true;
        }
    }
    

    如果没有,您可以通过注册网格的CellEditEnding 事件来执行相同操作,如下所示:

                mainGrid.CellEditEnding += (s, e) =>
                                           {
                                               e.Cancel = true;
                                           };
    

    【讨论】:

    • 感谢 Omribitan,但这个答案有问题。如果我设置 e.Cancel=true,DataGrid 将在用户离开 TexBoxDatagridTextColumn 后保持在编辑模式,并且它以后会引起很多问题。
    • @raha 正如我理解您的问题一样,当您向单元格输入无效值时,您正在寻找一种方法来阻止数据网格阻止您在其他单元格中进入编辑模式。我误解了吗?你现在在寻找别的东西吗?
    • 不,问题仍然相同,但正如我在之前的评论中所说,我无法使用您的解决方案,因为它会导致 datagrid 上出现其他问题。在我上一条评论中,我提到了这个问题。
    • 例如我有时会收到此错误:“在 AddNew 或 EditItem 事务期间不允许'排序'。”(当用户在编辑后离开 textbox 时,datagrid 应该被提交,但这不是因为我覆盖了OnCellEditEnding 方法并设置了e.Cancel = true)
    【解决方案2】:

    我重写了datagrid 的 OnCanExecuteBeginEdit 方法,如下所示,它现在可以工作了。

      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: On cell validation error other row cells are uneditable/Readonly

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多