WPF 如何对TextBox中输入的内容进行验证?

        其实,在日常生活中我们打开一个网站或者APP首先都会弹出登录或注册的界面。比如注册,有用户名和密码,当你输入用户名时,如何不符合它定义的格式就会有提示要重新输入。在下不才,新手上路,想记录一下目前项目中用到的验证。说是项目,其实就是一个tool,不过内容比较多,还是挺复杂的。好在客户公司研发中心把开发流程都已给出,所以基本就是对着流程实现相应功能即可。。好了,闲话不多说,直接讲问题。


        对于winform,基本上就是直接在界面对控件右击进入属性然后添加事件。可是这次我忽略了这是WPF,而且我的界面不是画出来的,关于尺寸等都是通过code实现的,犯了一个很愚蠢的错误:直接写事件操作,却忘记给控件添加事件。后果就是对控件进行操作却始终进不去事件函数。。好蠢的问题,不愧是新手!!!对自己无语了~这里用的是MouseLeave事件(winform和WPF的事件是有区别的),直接控件点出事件 +=再new即可。     


         这是第一次写博文,想记录一下自己犯的错误,提醒自己~~当然,本人也是小菜鸟,或许还算不上菜鸟,哈哈哭不过希望能利用这个平台记录一下遇到的问题,总结的知识点以及学到的新知识等,共勉吧~梦想还是要有的,万一实现了呢。




WPF,可以通过以下几种方法对TextBox进行验证: 1. 使用ValidationRule类:可以自定义一个ValidationRule类,然后将其绑定到TextBox的ValidationRules属性上,以实现对TextBox输入内容进行验证。例如: ```xaml <TextBox> <TextBox.Text> <Binding Path="UserName" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:UserNameValidationRule /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> ``` 其,local:UserNameValidationRule是自定义的ValidationRule类。 2. 使用IDataErrorInfo接口:可以在ViewModel实现IDataErrorInfo接口,然后将TextBox的Binding.Validation属性设置为True,以实现对TextBox输入内容进行验证。例如: ```xaml <TextBox Text="{Binding UserName, ValidatesOnDataErrors=True}" /> ``` 其,ViewModel实现IDataErrorInfo接口的代码如下: ```csharp public class UserViewModel : IDataErrorInfo { public string UserName { get; set; } public string Error => null; public string this[string propertyName] { get { string error = null; switch (propertyName) { case "UserName": if (string.IsNullOrEmpty(UserName)) error = "请输入用户名"; break; } return error; } } } ``` 3. 使用INotifyDataErrorInfo接口:与IDataErrorInfo类似,可以在ViewModel实现INotifyDataErrorInfo接口,然后将TextBox的Binding.Validation属性设置为True,以实现对TextBox输入内容进行验证。不同的是,INotifyDataErrorInfo可以实现异步验证。例如: ```xaml <TextBox Text="{Binding UserName, ValidatesOnNotifyDataErrors=True}" /> ``` 其,ViewModel实现INotifyDataErrorInfo接口的代码如下: ```csharp public class UserViewModel : INotifyDataErrorInfo { private string _userName; public string UserName { get { return _userName; } set { if (_userName != value) { _userName = value; ValidateUserName(); OnPropertyChanged("UserName"); } } } private readonly Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>(); public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged; public bool HasErrors => _errors.Any(); public IEnumerable GetErrors(string propertyName) { if (string.IsNullOrEmpty(propertyName)) return _errors.Values; return _errors.ContainsKey(propertyName) ? _errors[propertyName] : null; } private void ValidateUserName() { if (string.IsNullOrEmpty(UserName)) { AddError("UserName", "请输入用户名"); } else { RemoveError("UserName", "请输入用户名"); } } private void AddError(string propertyName, string error) { if (!_errors.ContainsKey(propertyName)) _errors[propertyName] = new List<string>(); if (!_errors[propertyName].Contains(error)) { _errors[propertyName].Add(error); ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); } } private void RemoveError(string propertyName, string error) { if (_errors.ContainsKey(propertyName) && _errors[propertyName].Contains(error)) { _errors[propertyName].Remove(error); if (_errors[propertyName].Count == 0) _errors.Remove(propertyName); ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); } } protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; } ``` 以上就是对WPFTextBox进行验证的三种方法的详细描述。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值