WPF数据绑定之ValidationRule数据校验综合Demo

WPF数据绑定之ValidationRule数据校验综合Demo

2023年6月24日发(作者:)

WPF数据绑定之ValidationRule数据校验综合DemoWPF 数据绑定之ValidationRule数据校验综合Demo⼀、概述我们利⽤ValidationRule以及ErrorTemplate来制作⼀个简单的表单验证。⼆、Demo核⼼思想:我们在ValidationRule中的Validate函数中进⾏验证,然后将验证结果存放⾄⼀个预先定义好的全局资源中,这样其他控件就可以根据验证结果来进⾏相应的处理,代码参见以下: using entModel; using ization; using rExpressions; using s; using ls;

namespace BindingDemo5ValidationRuleDemo { ///

/// Interaction logic for /// public partial class MainWindow : Window { private string nickName; public string NickName { get { return nickName; } set { nickName = value; } }

private string phoneNumber; public string PhoneNumber { get { return phoneNumber; } set { phoneNumber = value; } }

private string password; public string Password { get { return password; } set { password = value; } }

private string confirmPassword; public string ConfirmPassword { get { return confirmPassword; } set { confirmPassword = value; } }

public MainWindow() { InitializeComponent(); ntext = this; }

private void Button_Click(object sender, RoutedEventArgs e) { (NickName); } } }

public class ValidationOutput : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnNotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }

private bool isAllValid = true;//Indicate if all elements is valid

public bool IsAllValid { get { return isAllValid; } set { isAllValid = value; OnNotifyPropertyChanged("IsAllValid"); } }

private string errorMsg4NickName; public string ErrorMsg4NickName { get { return errorMsg4NickName; } set { errorMsg4NickName = value; OnNotifyPropertyChanged("ErrorMsg4NickName"); } }

private bool isShow4NickName; public bool IsShow4NickName { get { return isShow4NickName; } set { isShow4NickName = value; OnNotifyPropertyChanged("IsShow4NickName"); } }

private string errorMsg4PhoneNumber; public string ErrorMsg4PhoneNumber { get { return errorMsg4PhoneNumber; } set { errorMsg4PhoneNumber = value; OnNotifyPropertyChanged("ErrorMsg4PhoneNumber"); } }

private bool isShow4PhoneNumber; public bool IsShow4PhoneNumber { get { return isShow4PhoneNumber; } set { isShow4PhoneNumber = value; OnNotifyPropertyChanged("IsShow4PhoneNumber"); OnNotifyPropertyChanged("IsShow4PhoneNumber"); } }

private bool isPhoneNumberValid; public bool IsPhoneNumberValid { get { return isPhoneNumberValid; } set { isPhoneNumberValid = value; OnNotifyPropertyChanged("IsPhoneNumberValid"); } }

private bool isNickNameValid; public bool IsNickNameValid { get { return isNickNameValid; } set { isNickNameValid = value; OnNotifyPropertyChanged("IsNickNameValid"); } }

private string errorMsg4Password; public string ErrorMsg4Password { get { return errorMsg4Password; } set { errorMsg4Password = value; OnNotifyPropertyChanged("ErrorMsg4Password"); } }

private bool isPasswordValid; public bool IsPasswordValid { get { return isPasswordValid; } set { isPasswordValid = value; OnNotifyPropertyChanged("IsPasswordValid"); } }

private string password; public string Password { get { return password; } set { password = value; OnNotifyPropertyChanged("Password"); } }

private string errorMsg4ConfirmPassword; public string ErrorMsg4ConfirmPassword { get { return errorMsg4ConfirmPassword; } get { return errorMsg4ConfirmPassword; } set { errorMsg4ConfirmPassword = value; OnNotifyPropertyChanged("ErrorMsg4ConfirmPassword"); } }

private bool isConfirmPasswordValid; public bool IsConfirmPasswordValid { get { return isConfirmPasswordValid; } set { isConfirmPasswordValid = value; OnNotifyPropertyChanged("IsConfirmPasswordValid"); } }

}

public class RegisterValidationRule : ValidationRule { private ValidationOutput validationOutput = null; public ValidationOutput ValidationOutput { get { return validationOutput; } set { validationOutput = value; } } private string validateType;

public string ValidateType { get { return validateType; } set { validateType = value; } }

public bool IsLegalPhoneNumber(string phoneNumber) { return h(phoneNumber, @"^1[3578]d{9}$"); } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { if (ValidateType == "NickName") { string s = (string)value; if ( < 6) { NameValid = false; alid = false; sg4NickName = "Length should be longger than 6 characters"; return new ValidationResult(false, "Length should be longger than 6 characters"); } else { NameValid = true; alid = true; return new ValidationResult(true, null); }

} else if(ValidateType == "PhoneNumber") { string s = (string)value; if (!IsLegalPhoneNumber(s)) if (!IsLegalPhoneNumber(s)) { //4PhoneNumber = true; eNumberValid = false; alid = false; sg4PhoneNumber = "Phone number format is not correct"; return new ValidationResult(false, "Phone number format is not correct"); } else { 4PhoneNumber = false; eNumberValid = true; alid = true; return new ValidationResult(true, null); } } else if (ValidateType == "Password") { string myPassword = (string)value; rd = myPassword;//Store the password in a global resource, used for validating the confirm password if ( < 8) { wordValid = false; alid = false; sg4Password = "Password length should be longger than 8 characters"; return new ValidationResult(false, "Password length should be longger than 8 characters"); } else { wordValid = true; alid = true; return new ValidationResult(true, null); } } else if (ValidateType == "ConfirmPassword") { string myConfirmPassword = (string)value; string myPassword = rd; if (myPassword != myConfirmPassword) { irmPasswordValid = false; alid = false; sg4ConfirmPassword = "Password are not the same"; return new ValidationResult(false, "Password are not the same"); } else { irmPasswordValid = true; alid = true; return new ValidationResult(true, null); } } return new ValidationResult(true, null);

}

} }

ToolTip="{Binding ElementName=adorner, Path=AdornedElement.().ontent}"

>

<> <>

<> <>

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687607046a24160.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信