WPF-验证错误事件不会触发

2024-03-02

我想我已经阅读了所有相关文章,但没有一篇有帮助..

我正在尝试启用/禁用保存按钮datagrid通过错误状态 - 但没有成功。

这是我的代码:

承包商:

AddHandler(Validation.ErrorEvent, new RoutedEventHandler(OnErrorEvent));

XAML:

    <Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
 xmlns:local="clr-namespace:Metsuka_APP" x:Class="Metsuka_APP.MichlolimManagment" 
  mc:Ignorable="d" 
  d:DesignHeight="500" d:DesignWidth="500"
Title="MichlolimManagment"
x:Name="Michlolim_Managment" Validation.Error="Michlolim_Managment_Error">
<Page.Resources>

<DataGrid x:Name="AGAFIMDataGrid" VerticalAlignment="Center" RowEditEnding="rowEditEnding" Margin="10" FlowDirection="RightToLeft" Height="340"
    AutoGenerateColumns="False" EnableRowVirtualization="True"
                  ItemsSource="{Binding Source={StaticResource aGAFIMViewSource}}"   Grid.Row="1"
                  RowDetailsVisibilityMode="VisibleWhenSelected"
                 ScrollViewer.CanContentScroll="True"
                 ScrollViewer.VerticalScrollBarVisibility="Auto" 
                 HorizontalGridLinesBrush="Silver"
                 VerticalGridLinesBrush="Silver">
            <DataGrid.Resources>
                <Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
                    <Setter Property="Padding" Value="-2"/>
                    <Style.Triggers>
                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="ToolTip" 
          Value="{Binding RelativeSource={RelativeSource Self},
            Path=(Validation.Errors)[0].ErrorContent}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="agaf_nameColumn"  Header="name" Width="*">
                    <DataGridTextColumn.Binding>
                        <Binding Path="agaf_name" NotifyOnValidationError="True" >
                            <Binding.ValidationRules>
                            <local:MichlolimValidationRule ValidationStep="UpdatedValue"/>
                        </Binding.ValidationRules>
                    </Binding>
                        </DataGridTextColumn.Binding>
                </DataGridTextColumn>
            </DataGrid.Columns>
            <DataGrid.RowValidationErrorTemplate>
                <ControlTemplate>
                    <Grid Margin="0,-2,0,-2"
            ToolTip="{Binding RelativeSource={RelativeSource
            FindAncestor, AncestorType={x:Type DataGridRow}},
            Path=(Validation.Errors)[0].ErrorContent}">
                        <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>

后面的代码:

    private int errorCount;

    private void OnErrorEvent(object sender, RoutedEventArgs e)
    {
        var validationEventArgs = e as ValidationErrorEventArgs;
        if (validationEventArgs == null)
            throw new Exception("Unexpected event args");
        switch (validationEventArgs.Action)
        {
            case ValidationErrorEventAction.Added:
                {
                    errorCount++; break;
                }
            case ValidationErrorEventAction.Removed:
                {
                    errorCount--; break;
                }
            default:
                {
                    throw new Exception("Unknown action");
                }
        }
        btnSavePop.IsEnabled = errorCount == 0;
    }

but the "OnErrorEvent"从不点火——知道为什么吗?


你需要设置NotifyOnValidationError="True" https://msdn.microsoft.com/en-us/library/system.windows.data.binding.notifyonvalidationerror%28v=vs.110%29.aspx在您的绑定上 - 否则,将不会引发该事件。我建议使用IDataErrorInfo or INotifyDataErrorInfo接口而不是 MVVM 错误处理方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF-验证错误事件不会触发 的相关文章

  • 进入嵌入式[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • ScrollableControl 在整个控件周围绘制边框

    我正在构建基于的自定义用户控件ScrollableControl 现在我正在尝试在控件周围添加边框 类似于 DataGridView 的边框 我可以使用以下方法绘制边框 e Graphics TranslateTransform AutoS
  • C# SMO 远程数据库备份到本地机器

    我有一个执行 SQL 数据库备份和恢复的应用程序 这在本地计算机上运行良好 但是如果我针对另一台计算机上托管的 SQL 服务器运行此应用程序 则会出现以下错误 Microsoft SqlServer Management Smo Faile
  • 有什么办法可以让这个 C# 代码更快吗? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在读取一个大文件 X12 并解析其中的信息 我有两个瓶颈功能 我似乎无法解决 read line 和 get element 有什
  • C++ 中的反向迭代器和负跨步迭代器,在开始之前使用一个作为哨兵

    In 查看 C 反向迭代器的另一种方式 https devblogs microsoft com oldnewthing 20211112 00 p 105908雷蒙德 陈写道 C 语言的一个怪癖 你可以拥有一个指针 集合的 已过终点 但不
  • 增强缓冲区调用后丢失自定义点类型的数据

    我有我自己的观点 class LocationWayPoint public latlong container location WORD index PWeakBasicStation station namespace boost n
  • 有没有办法使用 ews c# 确定电子邮件是否是回复/响应?

    我正在编写一个支持系统 这是我第一次使用 EWS 到目前为止 我已经相当成功了 我可以提取我需要的信息 发送电子邮件 一切正常 我确实有点头疼 有没有办法判断电子邮件是否实际上是回复 该应用程序的基本思想是有人发送电子邮件 我们回复并给他们
  • Java,根据 WSDL 中的嵌入模式验证 XML

    我有一个 wsdl具有嵌入架构的文件 我想使用它来验证 XML 文件 字符串 wsdl文件 与验证的方式相同 xsd 该架构位于
  • FxCop 和 GAC 疯狂

    当我尝试分析依赖于模式和实践 企业库数据 以及其他 2 0 0 0 的项目时使用 FxCop FxCop 抱怨它不能 定位程序集引用 即使正在分析的应用程序 dll 是根据其编译的此版本及其在 GAC 中 如果我浏览到 GAC 尝试选择相同
  • std::istringstream >> 使奇怪的行为加倍

    下面的代码打印0在 mac osx 上使用 clang 其他地方都会打印5 clang https ideone com mVgpzS gcc https ideone com oZ0hy6 include
  • 安全移动 C++ 对象

    我听到过一些警告 不要通过以下方式将对象运送到另一个内存位置memcpy 但不知道具体原因 除非它包含的成员做了依赖于内存位置的棘手事情 否则这应该是完全安全的 或者不是 编辑 预期的用例是像这样的数据结构vector 它存储对象 不是po
  • 从 ASP.NET Web API 返回 HTML

    如何从 ASP NET MVC Web API 控制器返回 HTML 我尝试了下面的代码 但由于未定义 Response Write 而出现编译错误 public class MyController ApiController HttpP
  • IOS Box2D - 身体遵循基于速度波动的点数组的特定路径

    我有一个关于身体的问题 它遵循特定的路径 首先是将身体移动到目标点的方法 const float destinationControl 0 3f b2Vec2 targetPosition path counter b2Vec2 missi
  • 在 C++ 中运行 python [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我有一个用 C 编写的应用程序和一个测试系统 也是用 C 编写的 测试系统非常复杂并且很难改变 我只想做一些小的改变 我的班级是这样的
  • 我们可以向 ServicePointManager.SecurityProtocol 添加四个协议吗?

    我想支持从 ssl3 到 tls 1 2 的所有安全协议 但是在网上搜索时我发现代码为 ServicePointManager SecurityProtocol SecurityProtocolType Ssl3 SecurityProto
  • 在运行时生成可执行文件

    好吧 所以我想知道如何创建一个程序 该程序创建第二个程序 就像大多数压缩程序如何创建自解压自可执行文件一样 但这不是我需要的 假设我有 2 个程序 每个都包含一个类 我将使用一个程序来修改类并用数据填充类 第二个文件将是一个也具有该类的程序
  • 如何通过反射获取透明代理的属性值?

    我的代码接收透明代理而不是原始实例 虽然这var type obj GetType 产生原始类的类型 下面的代码抛出TargetException 对象与目标类型不匹配 var value property GetValue obj nul
  • 通过 C++ 标头在 C++ 和 C# 中使用枚举

    我有一个用 C 编写的服务器 位于命名管道的末端 嗯 提供服务 可以发送到服务器的命令在位于头文件中的枚举中定义 enum e doThing1 e doThing2 e doLastThing 所需枚举的值被放入发送到服务器的消息的第一个
  • 如何使用 XmlSerializer 生成标记前缀

    我想使用 XmlSerializer 生成以下内容
  • 从 C# 应用程序调用 ASP.net Web 服务

    我有个问题 我如何调用 Web 服务并从 C 桌面应用程序获取结果 我正在制作一个桌面应用程序 我希望它能够连接到我的在线 ASP net Web 服务 这怎么可能 在 解决方案资源管理器 中 右键单击项目节点并选择 添加 Service参

随机推荐