【问题标题】:How I can edit next cell in DataGrid after the error?错误后如何在 DataGrid 中编辑下一个单元格?
【发布时间】:2014-05-07 12:01:16
【问题描述】:

我的 DataGrid 有问题,它用红色标记边框,错误地输入了单元格。我读到申请很有用

<!------------------------     XAML     ------------------------------->

    <DataGridTextColumn.EditingElementStyle>
      <Style>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
         <!-- no red border round cell-->
      </Style>
    </DataGridTextColumn.EditingElementStyle>

<!------------------------     XAML     ------------------------------->

右边的边框是白色的,但我仍然无法编辑 DataGrid 中的下一个单元格。如何解决这个问题?

【问题讨论】:

  • 我认为正确的解决方案是取消对错误行的编辑,但现在我没有时间获得经过全面测试的答案。

标签: c# wpf datagrid


【解决方案1】:

如果这遵循 MVVM 结构并且如果它重复多次,那么最好的办法是拥有自己的 DataGrid,它继承自另一个 DataGrid。那么您将覆盖OnCellEditEnding 方法,例如:

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

或者更疯狂的方式是这样的:

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

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 2013-09-19
    • 2012-12-04
    • 2018-12-06
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多