一、概述
我们利用ValidationRule以及ErrorTemplate来制作一个简单的表单验证。
二、Demo
核心思想:我们在ValidationRule中的Validate函数中进行验证,然后将验证结果存放至一个预先定义好的全局资源中,这样其他控件就可以根据验证结果来进行相应的处理,代码参见以下:
1 using System.ComponentModel; 2 using System.Globalization; 3 using System.Text.RegularExpressions; 4 using System.Windows; 5 using System.Windows.Controls; 6 7 namespace BindingDemo5ValidationRuleDemo 8 { 9 /// <summary> 10 /// Interaction logic for MainWindow.xaml 11 /// </summary> 12 public partial class MainWindow : Window 13 { 14 private string nickName; 15 public string NickName 16 { 17 get { return nickName; } 18 set { nickName = value; } 19 } 20 21 private string phoneNumber; 22 public string PhoneNumber 23 { 24 get { return phoneNumber; } 25 set { phoneNumber = value; } 26 } 27 28 private string password; 29 public string Password 30 { 31 get { return password; } 32 set { password = value; } 33 } 34 35 private string confirmPassword; 36 public string ConfirmPassword 37 { 38 get { return confirmPassword; } 39 set { confirmPassword = value; } 40 } 41 42 public MainWindow() 43 { 44 InitializeComponent(); 45 this.DataContext = this; 46 } 47 48 private void Button_Click(object sender, RoutedEventArgs e) 49 { 50 MessageBox.Show(NickName); 51 } 52 } 53 54 public class ValidationOutput : INotifyPropertyChanged 55 { 56 public event PropertyChangedEventHandler PropertyChanged; 57 public void OnNotifyPropertyChanged(string propertyName) 58 { 59 if (PropertyChanged != null) 60 { 61 PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 62 } 63 } 64 65 private bool isAllValid = true;//Indicate if all elements is valid 66 67 public bool IsAllValid 68 { 69 get { return isAllValid; } 70 set { isAllValid = value; 71 OnNotifyPropertyChanged("IsAllValid"); 72 } 73 } 74 75 76 private string errorMsg4NickName; 77 public string ErrorMsg4NickName 78 { 79 get { return errorMsg4NickName; } 80 set 81 { 82 errorMsg4NickName = value; 83 OnNotifyPropertyChanged("ErrorMsg4NickName"); 84 } 85 } 86 87 private bool isShow4NickName; 88 public bool IsShow4NickName 89 { 90 get { return isShow4NickName; } 91 set 92 { 93 isShow4NickName = value; 94 OnNotifyPropertyChanged("IsShow4NickName"); 95 } 96 } 97 98 99 private string errorMsg4PhoneNumber; 100 public string ErrorMsg4PhoneNumber 101 { 102 get { return errorMsg4PhoneNumber; } 103 set 104 { 105 errorMsg4PhoneNumber = value; 106 OnNotifyPropertyChanged("ErrorMsg4PhoneNumber"); 107 } 108 } 109 110 private bool isShow4PhoneNumber; 111 public bool IsShow4PhoneNumber 112 { 113 get { return isShow4PhoneNumber; } 114 set 115 { 116 isShow4PhoneNumber = value; 117 OnNotifyPropertyChanged("IsShow4PhoneNumber"); 118 } 119 } 120 121 private bool isPhoneNumberValid; 122 public bool IsPhoneNumberValid 123 { 124 get { return isPhoneNumberValid; } 125 set 126 { 127 isPhoneNumberValid = value; 128 OnNotifyPropertyChanged("IsPhoneNumberValid"); 129 } 130 } 131 132 private bool isNickNameValid; 133 public bool IsNickNameValid 134 { 135 get { return isNickNameValid; } 136 set 137 { 138 isNickNameValid = value; 139 OnNotifyPropertyChanged("IsNickNameValid"); 140 } 141 } 142 143 144 145 private string errorMsg4Password; 146 public string ErrorMsg4Password 147 { 148 get { return errorMsg4Password; } 149 set 150 { 151 errorMsg4Password = value; 152 OnNotifyPropertyChanged("ErrorMsg4Password"); 153 } 154 } 155 156 private bool isPasswordValid; 157 public bool IsPasswordValid 158 { 159 get { return isPasswordValid; } 160 set 161 { 162 isPasswordValid = value; 163 OnNotifyPropertyChanged("IsPasswordValid"); 164 } 165 } 166 167 private string password; 168 public string Password 169 { 170 get { return password; } 171 set 172 { 173 password = value; 174 OnNotifyPropertyChanged("Password"); 175 } 176 } 177 178 179 private string errorMsg4ConfirmPassword; 180 public string ErrorMsg4ConfirmPassword 181 { 182 get { return errorMsg4ConfirmPassword; } 183 set 184 { 185 errorMsg4ConfirmPassword = value; 186 OnNotifyPropertyChanged("ErrorMsg4ConfirmPassword"); 187 } 188 } 189 190 private bool isConfirmPasswordValid; 191 public bool IsConfirmPasswordValid 192 { 193 get { return isConfirmPasswordValid; } 194 set 195 { 196 isConfirmPasswordValid = value; 197 OnNotifyPropertyChanged("IsConfirmPasswordValid"); 198 } 199 } 200 201 202 } 203 204 public class RegisterValidationRule : ValidationRule 205 { 206 private ValidationOutput validationOutput = null; 207 public ValidationOutput ValidationOutput 208 { 209 get { return validationOutput; } 210 set { validationOutput = value; } 211 } 212 private string validateType; 213 214 public string ValidateType 215 { 216 get { return validateType; } 217 set { validateType = value; } 218 } 219 220 public bool IsLegalPhoneNumber(string phoneNumber) 221 { 222 return Regex.IsMatch(phoneNumber, @"^1[3578]\d{9}$"); 223 } 224 public override ValidationResult Validate(object value, CultureInfo cultureInfo) 225 { 226 if (ValidateType == "NickName") 227 { 228 string s = (string)value; 229 if (s.Length < 6) 230 { 231 ValidationOutput.IsNickNameValid = false; 232 ValidationOutput.IsAllValid = false; 233 ValidationOutput.ErrorMsg4NickName = "Length should be longger than 6 characters"; 234 return new ValidationResult(false, "Length should be longger than 6 characters"); 235 } 236 else 237 { 238 ValidationOutput.IsNickNameValid = true; 239 ValidationOutput.IsAllValid = true; 240 return new ValidationResult(true, null); 241 } 242 243 } 244 else if(ValidateType == "PhoneNumber") 245 { 246 string s = (string)value; 247 if (!IsLegalPhoneNumber(s)) 248 { 249 //ValidationOutput.IsShow4PhoneNumber = true; 250 ValidationOutput.IsPhoneNumberValid = false; 251 ValidationOutput.IsAllValid = false; 252 ValidationOutput.ErrorMsg4PhoneNumber = "Phone number format is not correct"; 253 return new ValidationResult(false, "Phone number format is not correct"); 254 } 255 else 256 { 257 ValidationOutput.IsShow4PhoneNumber = false; 258 ValidationOutput.IsPhoneNumberValid = true; 259 ValidationOutput.IsAllValid = true; 260 return new ValidationResult(true, null); 261 } 262 } 263 else if (ValidateType == "Password") 264 { 265 string myPassword = (string)value; 266 ValidationOutput.Password = myPassword;//Store the password in a global resource, used for validating the confirm password 267 if (myPassword.Length < 8) 268 { 269 ValidationOutput.IsPasswordValid = false; 270 ValidationOutput.IsAllValid = false; 271 ValidationOutput.ErrorMsg4Password = "Password length should be longger than 8 characters"; 272 return new ValidationResult(false, "Password length should be longger than 8 characters"); 273 } 274 else 275 { 276 ValidationOutput.IsPasswordValid = true; 277 ValidationOutput.IsAllValid = true; 278 return new ValidationResult(true, null); 279 } 280 } 281 else if (ValidateType == "ConfirmPassword") 282 { 283 string myConfirmPassword = (string)value; 284 string myPassword = ValidationOutput.Password; 285 if (myPassword != myConfirmPassword) 286 { 287 ValidationOutput.IsConfirmPasswordValid = false; 288 ValidationOutput.IsAllValid = false; 289 ValidationOutput.ErrorMsg4ConfirmPassword = "Password are not the same"; 290 return new ValidationResult(false, "Password are not the same"); 291 } 292 else 293 { 294 ValidationOutput.IsConfirmPasswordValid = true; 295 ValidationOutput.IsAllValid = true; 296 return new ValidationResult(true, null); 297 } 298 } 299 return new ValidationResult(true, null); 300 301 } 302 303 } 304 }