即使行具有错误验证规则,也继续添加或编辑数据网格行

编程入门 行业动态 更新时间:2024-10-24 22:28:22
本文介绍了即使行具有错误验证规则,也继续添加或编辑数据网格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望datagrid继续添加或编辑DataGrid行,即使行有错误验证规则 这是样本以澄清想法

I want datagrid to continue Adding or Editing DataGrid row even if row has error validation rule this is sample in order to clarify the idea

<DataGrid Name="Dgrid" AutoGenerateColumns="False" ItemsSource="{Binding Request.Obligations}" CanUserAddRows="True"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Id}" /> <DataGridTextColumn Binding="{Binding Name}"/> <DataGridTextColumn Binding="{Binding Age}"/> </DataGrid.Columns> <DataGrid.RowValidationRules> <validationRules:ٌRowValidationRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue" /> </DataGrid.RowValidationRules> <DataGrid.RowValidationErrorTemplate> <ControlTemplate> <!--<Grid Margin="0,-2,0,-2" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},Path=(Validation.Errors)[0].ErrorContent}">--> <Grid Margin="0,-2,0,-2"> <Grid.ToolTip> <MultiBinding Converter="{StaticResource DataGridRowMultiConverter}"> <Binding Path="(Validation.Errors)[0].ErrorContent" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}" /> <Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" /> </MultiBinding> </Grid.ToolTip> <Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}" Height="{TemplateBinding FontSize}" /> <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center" /> </Grid> </ControlTemplate> </DataGrid.RowValidationErrorTemplate> </DataGrid>

如果用户输入了包含无效数据的新行,他将无法添加新行新行将丢失,现有数据无法编辑。 简而言之,我想停止RowValidationRules的行为并使其jop告诉用户他已输入无效数据而没有阻止他。 搜索后我发现此代码:

If a user entered a new row with invalid data, he will not be able to add a new row and the new row will be missing and the existing data can not be edited. In short, I want to stop the behavior of RowValidationRules and make its jop to tell the user that he has entered invalid data without stoping him. After search i found this code:

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)cellError.GetValue(this, null); if (rowError != null) hasRowValidationError = (bool)rowError.GetValue(this, null); base.OnCanExecuteBeginEdit(e); if ((!e.CanExecute && hasCellValidationError) || (!e.CanExecute && hasRowValidationError)) { e.CanExecute = true; e.Handled = true; } }

但我发现我可以编辑行但无法添加更多行 我尝试过: 继续添加或编辑DataGrid行,即使行有错误验证规则

but I found that I can edit rows but can't add more rows What I have tried: continue Adding or Editing DataGrid row even if row has error validation rule

推荐答案

Quote:

继续添加或编辑datagrid行,即使行有错误验证规则

Continue adding or editing datagrid row even if row has error validation rule

如果你不想要尊重它,为什么还需要验证规则呢? 最简单的解决方案是不是验证规则。

Why do you want a validation rules if you don't want to have to respect it ? The easiest solution is not validation rule.

更多推荐

即使行具有错误验证规则,也继续添加或编辑数据网格行

本文发布于:2023-11-23 03:29:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   规则   错误   编辑   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!