当前位置: 首页 > article >正文

WPF点击提交按钮后验证

1.定义ValidateModelBase 基类,实现IDataErrorInfo接口来触发验证信息,

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using CommunityToolkit.Mvvm.ComponentModel;
 
   /// <summary>
   /// 验证模型基类
   /// </summary>
   public class ValidateModelBase : ObservableObject, IDataErrorInfo
   {
       public ValidateModelBase()
       {
           DataErrors = new Dictionary<string, string>();
       }

       #region 属性

       /// <summary>
       /// 表当验证错误集合
       /// </summary>
       public Dictionary<string, string>? DataErrors { get; private set; } = new Dictionary<string, string>();

       /// <summary>
       /// 是否验证通过
       /// </summary>
       public bool IsValidated
       {
           get
           {
               return DataErrors?.Count == 0;
           }
       }

       //是否允许验证
       private bool _shouldValidate = false;

       #endregion 属性

       public string this[string columnName]
       {
           get
           {
               if (!_shouldValidate)
                   return string.Empty;

               if (columnName == null) throw new ArgumentNullException(nameof(columnName));

               ValidationContext vc = new ValidationContext(this, null, null)
               {
                   MemberName = columnName
               };

               var res = new List<System.ComponentModel.DataAnnotations.ValidationResult>();

               var propertyInfo = this.GetType().GetProperty(columnName);
               if (propertyInfo == null)
               {
                   return string.Empty;
               }

               var value = propertyInfo.GetValue(this, null);
               bool result = Validator.TryValidateProperty(value, vc, res);

               if (res.Count > 0)
               {
                   string errorInfo = string.Join(Environment.NewLine, res.Select(r => r.ErrorMessage).ToArray());
                   AddDic(DataErrors, columnName, errorInfo);
                   return errorInfo;
               }

               RemoveDic(DataErrors, columnName);
               return string.Empty;
           }
       }

       public string Error => string.Empty;

       #region 附属方法

       /// <summary>
       /// 移除字典
       /// </summary>
       private void RemoveDic(Dictionary<string, string>? dics, string dicKey)
       {
           if (dics != null)
           {
               dics.Remove(dicKey);
           }
       }

       /// <summary>
       /// 添加字典
       /// </summary>
       private void AddDic(Dictionary<string, string>? dics, string dicKey, string dicValue)
       {
           if (dics != null && !dics.ContainsKey(dicKey))
           {
               dics.Add(dicKey, dicValue);
           }
       }

       #endregion 附属方法

       /// <summary>
       /// 执行验证
       /// </summary>
       /// <returns></returns>
       public bool Validate()
       {
           _shouldValidate = true;
           this.GetType().GetProperties()
           .Where(prop => prop.GetCustomAttributes(false)
           .Any(attr => attr.GetType().Namespace == typeof(RequiredAttribute).Namespace))
           .ToList().ForEach(p => OnPropertyChanged(p.Name));
           //  this.GetType().GetProperties().ToList().ForEach(p => OnPropertyChanged(p.Name));
           return IsValidated;
       }
   }

http://www.kler.cn/a/537953.html

相关文章:

  • UE5.5 PCGFrameWork--GPU CustomHLSL
  • deepseek+kimi自动生成ppt
  • U3D支持webgpu阅读
  • verilog练习:i2c slave 模块设计
  • [LUA ERROR] bad light userdata pointer
  • 2025年软件测试五大趋势:AI、API安全、云测试等前沿实践
  • Lua语言的嵌入式系统
  • node.js + html + Sealos容器云 搭建简易多人实时聊天室demo 带源码
  • 知识点(SSTI漏洞/flask框架/tonado框架)
  • WPS如何接入DeepSeek(通过JS宏调用)
  • 在 Navicat 17 中扩展 PostgreSQL 数据类型 | 创建自定义域
  • 二叉树理论基础详解:从零开始理解数据结构的核心
  • react 路由配置:从入门到精通
  • 美的java面试经验
  • docker compose 文件详解
  • 【含文档+PPT+源码】基于Python校园跑腿管理系统设计与实现
  • 网络安全治理架构图 网络安全管理架构
  • upx压缩工具使用说明
  • STC51 单片机中,定时器 / 计数器相关的寄存器
  • CNN 卷积神经网络处理图片任务 | PyTorch 深度学习实战
  • R包:ggalign调整和组合多个图形的R包
  • 判断您的Mac当前使用的是Zsh还是Bash:echo $SHELL、echo $0
  • java基础4(黑马)
  • 语言月赛 202308【小粉兔做麻辣兔头】题解(AC)
  • TypeScript 中的元组:固定长度的数组
  • 论软件架构风格论文