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

wpf devexpress 使用IDataErrorInfo实现input验证

此处下载源码

当form初始化显示,Register按钮应该启动和没有输入错误应该显示。如果用户点击注册按钮在特定的输入无效数据,form将显示输入错误和禁用的注册按钮。实现逻辑在标准的IDataErrorInfo接口。请查阅IDataErrorInfo接口(System.ComponentModel)MSDN文章

查阅IDataErrorInfo接口实现在RegistrationViewModel类

[POCOViewModel]
public class RegistrationViewModel : IDataErrorInfo {...string IDataErrorInfo.Error {get {IDataErrorInfo me = (IDataErrorInfo)this;string error =me[BindableBase.GetPropertyName(() => FirstName)] +me[BindableBase.GetPropertyName(() => LastName)] +me[BindableBase.GetPropertyName(() => Email)] +me[BindableBase.GetPropertyName(() => Password)] +me[BindableBase.GetPropertyName(() => ConfirmPassword)] +me[BindableBase.GetPropertyName(() => Birthday)];if (!string.IsNullOrEmpty(error))return "Please check inputted data.";return null;}}string IDataErrorInfo.this[string columnName] {get {string firstNameProp = BindableBase.GetPropertyName(() => FirstName);string lastNameProp = BindableBase.GetPropertyName(() => LastName);string emailProp = BindableBase.GetPropertyName(() => Email);string passwordProp = BindableBase.GetPropertyName(() => Password);string confirmPasswordProp = BindableBase.GetPropertyName(() => ConfirmPassword);string birthdayProp = BindableBase.GetPropertyName(() => Birthday);string genderProp = BindableBase.GetPropertyName(() => Gender);if (columnName == firstNameProp) {if (FirstName == null || string.IsNullOrEmpty(FirstName))return string.Format("You cannot leave the {0} field empty.", firstNameProp);} else if (columnName == lastNameProp) {if (LastName == null || string.IsNullOrEmpty(LastName))return string.Format("You cannot leave the {0} field empty.", lastNameProp);} else if (columnName == emailProp) {if (Email == null || string.IsNullOrEmpty(Email))return string.Format("You cannot leave the {0} field empty.", emailProp);} else if (columnName == passwordProp) {if (Password == null || string.IsNullOrEmpty(Password))return string.Format("You cannot leave the {0} field empty.", passwordProp);} else if (columnName == confirmPasswordProp) {if (!string.IsNullOrEmpty(Password) && Password != ConfirmPassword)return "These passwords do not match. Please try again.";} else if (columnName == birthdayProp) {if (Birthday == null || string.IsNullOrEmpty(Birthday.ToString()))return string.Format("You cannot leave the {0} field empty.", birthdayProp);} else if (columnName == genderProp) {if (Gender == -1)return string.Format("You cannot leave the {0} field empty.", genderProp);}return null;}}
}

启动IDataErrorInfo验证在XAML设置Binding.ValidatesOnDataErrors参数为true。设置绑定参数对于每一个form内的编辑器,包括ConfirmPassword编辑器

<dxlc:LayoutControl ... >...<dxe:TextEdit NullText="FIRST" ValidateOnEnterKeyPressed="True" ValidateOnTextInput="False"><dxe:TextEdit.EditValue><Binding Path="FirstName" ValidatesOnDataErrors="True"UpdateSourceTrigger="PropertyChanged" Mode="TwoWay"/></dxe:TextEdit.EditValue></dxe:TextEdit>...<dxe:PasswordBoxEdit EditValue="{Binding ConfirmPassword, ValidatesOnDataErrors=True}" ValidateOnEnterKeyPressed="True" ValidateOnTextInput="True"/>...
</dxlc:LayoutControl>

如果现在运行sample,将验证error当应用程序开始。

此问题是相关于输入验证IDataErrorInfo接口实现。修复此问题,重要验证错误没有返回在ViewModel如果一个用户没有点击注册按钮。

[POCOViewModel]
public class RegistrationViewModel : IDataErrorInfo {...public void AddEmployee() {string error = EnableValidationAndGetError();if(error != null) return;EmployeesModelHelper.AddNewEmployee(FirstName, LastName, Email, Password, Birthday.Value);}bool allowValidation = false;string EnableValidationAndGetError() {allowValidation = true;string error = ((IDataErrorInfo)this).Error;if(!string.IsNullOrEmpty(error)) {this.RaisePropertiesChanged();return error;}return null;}string IDataErrorInfo.Error {get {if(!allowValidation) return null;...}}string IDataErrorInfo.this[string columnName] {get {if(!allowValidation) return null;...}}
}

RegistrationViewModel没有返回一个错误直到用户点击注册按钮。输入数据有一个错误如果用户点击Register,不需要点击记录执行ViewModel验证逻辑在EnableValidationAndGetError方法。注意EnableValidationAndGetError调用RaisePropertiesChanged。此方法通常调用指南潜在数据更改;在这种情况,一个直线需要初始化验证进程。

验证几乎完成。剩余问题是Password字段。当用户修改Password字段,ConfirmPassword字段没有反应。你可以调用RaisePropertyChanged方法对于ConfirmPassword字段当Password字段更改。

[POCOViewModel]
public class RegistrationViewModel : IDataErrorInfo {...public virtual string Password { get; set; }public virtual string ConfirmPassword { get; set; }...protected void OnPasswordChanged() {this.RaisePropertyChanged(x => x.ConfirmPassword);}...
}

必须显示一个消息指示当注册成功和失败。DevExpress.Xpf.Mvvm库提供一个服务机制在Mvvm支持这些任务。

使用服务,首先需要定义一个服务显示消息框。DXMessageBoxService已经定义在MainView等级。取回服务从RegistrationViewModel,使用GetService<T>扩展方法。

[POCOViewModel]
public class RegistrationViewModel : IDataErrorInfo {...public void AddEmployee() {string error = EnableValidationAndGetError();if(error != null) {OnValidationFailed(error);return;}EmployeesModelHelper.AddNewEmployee(FirstName, LastName, Email, Password, Birthday.Value);OnValidationSucceeded();}void OnValidationSucceeded() {this.GetService<IMessageBoxService>().Show("Registration succeeded", "Registration Form", MessageBoxButton.OK);}void OnValidationFailed(string error) {this.GetService<IMessageBoxService>().Show("Registration failed. " + error, "Registration Form", MessageBoxButton.OK);}...
}

代码之上声明两个方法-OnValidationSucceeded和OnValidationFailed-调用验证成功和失败,分别。这些方法获得IMessageBoxService服务定义在视图。服务接口提供Show方法显示方法框。

结果显示如下。

用户离开编辑框空白区域或者无效输入数据,这些相应消息将显示。

输入数据正确,用户通知注册成功完成。

此时,注册form是对于所有意图和目的,完成。

http://www.lryc.cn/news/252972.html

相关文章:

  • shell_81.Linux在命令行中创建使用函数
  • 鱼香ROS一键安装命令(支持微信、docker、ros等)
  • 深入理解 Go 函数:从基础到高级
  • 开启三层交换机DHCP服务
  • jspdf+html2canvas浏览器缩放问题
  • 西南科技大学模拟电子技术实验六(BJT电压串联负反馈放大电路)预习报告
  • JS的监听事件
  • JS Object.values()
  • 基于Java SSM人力资源管理系统
  • 人工智能和程序员
  • Unity优化篇:对于unity DrawCall/Mesh/纹理压缩/内存等方面的常规调试和优化手段
  • 学生信息管理系统
  • 纯代码压缩WordPress前端Html
  • Elasticsearch分词器--空格分词器(whitespace analyzer)
  • 【LeetCode】692. 前K个高频单词
  • 在Windows操作系统上使用rtsp simple server和ffmpeg推送录屏视频流
  • 互联网摸鱼日报(2023-12-05)
  • Android 项目的依赖方式
  • ArcGIS提取DEM中的山脉范围
  • 漏洞复现--万户ezoffice wpsservlet任意文件上传
  • TCPDUMP抓包明确显示IP地址和端口号
  • java FTP客户端获取文件流假死问题
  • python使用记录
  • 【Vulnhub 靶场】【Coffee Addicts: 1】【简单-中等】【20210520】
  • codeforces每日两道思维题(第 二 天)
  • 【网络安全】-常见的网站攻击方式详解
  • ElasticSearch学习笔记(一)
  • go写文件后出现大量NUL字符问题记录
  • 【Collection - PriorityQueue源码解析】
  • Javascript_根据截止日期超时自动返回