public override void OnApplyTemplate() { base.OnApplyTemplate(); switch (this.ValidateTrigger) { case EnumValidateTrigger.PropertyChanged: this.TextChanged += NumbericTextBox_TextChanged; this.LostFocus -= NumbericTextBox_LostFocus; break; case EnumValidateTrigger.LostFocus: this.TextChanged -= NumbericTextBox_TextChanged; this.LostFocus += NumbericTextBox_LostFocus; break; } //this.PART_ErrorPath = this.GetTemplateChild("PART_ErrorPath") as UIElement; //if(this.PART_ErrorPath != null) //{ // this.PART_ErrorPath.MouseEnter += (o, e) => // { // this.IsShowErrorTip = true; // }; // this.PART_ErrorPath.MouseLeave += (o, e) => // { // this.IsShowErrorTip = false; // }; //} NumbericTextBox.SetPatternAndTip(this.PatternType, this, true); }
private static void IsStartValidateCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { NumbericTextBox textBox = d as NumbericTextBox; if ((bool)e.NewValue) { textBox.ValidateTextBox(); } }
private static void SetPatternAndTip(EnumPatternType patternType, NumbericTextBox textBox, bool isFirstLoad) { switch (patternType) { case EnumPatternType.NotEmpty: textBox.Pattern = "\\S"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "不能为空" : textBox.ErrorContent; break; case EnumPatternType.OnlyNumber: textBox.Pattern = "^[0-9]*$"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "只能输入数字" : textBox.ErrorContent; break; case EnumPatternType.IPV4: textBox.Pattern = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "IP地址不正确,请输入正确的IPV4地址" : textBox.ErrorContent; break; case EnumPatternType.IPV6: textBox.Pattern = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "IP地址不正确,请输入正确的IPV6地址" : textBox.ErrorContent; break; case EnumPatternType.Email: textBox.Pattern = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "邮件地址不正确" : textBox.ErrorContent; break; case EnumPatternType.IdCard15: textBox.Pattern = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "身份证格式不正确" : textBox.ErrorContent; break; case EnumPatternType.IdCard18: textBox.Pattern = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "身份证格式不正确" : textBox.ErrorContent; break; case EnumPatternType.MobilePhone: textBox.Pattern = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "手机号码输入不正确" : textBox.ErrorContent; break; case EnumPatternType.Telephone: textBox.Pattern = "d{3}-d{8}|d{4}-d{7}"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "电话号码输入不正确" : textBox.ErrorContent; break; case EnumPatternType.OnlyChinese: textBox.Pattern = "^[\\u4e00-\\u9fa5]{0,}$"; textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "只能输入中文" : textBox.ErrorContent; break; } }
private static void ValidateTriggerCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { NumbericTextBox textBox = d as NumbericTextBox; switch ((EnumValidateTrigger)e.NewValue) { case EnumValidateTrigger.PropertyChanged: textBox.LostFocus -= textBox.NumbericTextBox_LostFocus; textBox.TextChanged += textBox.NumbericTextBox_TextChanged; break; case EnumValidateTrigger.LostFocus: textBox.LostFocus += textBox.NumbericTextBox_LostFocus; textBox.TextChanged -= textBox.NumbericTextBox_TextChanged; break; } }
private static void PatternTypeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { NumbericTextBox textBox = d as NumbericTextBox; SetPatternAndTip((EnumPatternType)e.NewValue, textBox, false); }