ValidateTextBox.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Controls.Primitives;
  6. namespace CustomUI
  7. {
  8. /// <summary>
  9. /// 自带数据校验的文本输入框
  10. /// 目前已有数据校验如下:
  11. /// 1、为空判断:IsRequired、RequiredMessage
  12. /// 2、数字校验:IsNumber、NumberMessage
  13. /// 3、号码校验:IsPhoneNumber、PhoneNumberMessage
  14. /// </summary>
  15. /// <remarks>add by zhidf 2016.7.31</remarks>
  16. public class ValidateTextBox : TextBox
  17. {
  18. private bool mIsValidatePass;
  19. static ValidateTextBox()
  20. {
  21. DefaultStyleKeyProperty.OverrideMetadata(typeof(ValidateTextBox), new FrameworkPropertyMetadata(typeof(ValidateTextBox)));
  22. }
  23. public ValidateTextBox() : base()
  24. {
  25. this.LostFocus += ValidateTextBox_LostFocus;
  26. this.Loaded += ValidateTextBox_Loaded;
  27. this.IsEnabledChanged += ValidateTextBox_IsEnabledChanged;
  28. }
  29. private void ValidateTextBox_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
  30. {
  31. if(this.IsEnabled)
  32. {
  33. }
  34. }
  35. private void ValidateTextBox_Loaded(object sender, RoutedEventArgs e)
  36. {
  37. switch (this.ValidateType)
  38. {
  39. case EnumValidateType.Loaded:
  40. this.BeginValidate();
  41. break;
  42. case EnumValidateType.LostFocus:
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. private void ValidateTextBox_LostFocus(object sender, RoutedEventArgs e)
  49. {
  50. this.BeginValidate();
  51. }
  52. private void BeginValidate()
  53. {
  54. if (this.IsRequired && this.CheckIsEmpty())
  55. {
  56. this.ShowError(this.RequiredMessage);
  57. return;
  58. }
  59. if (this.IsNumber && !this.CheckIsNumber())
  60. {
  61. this.ShowError(this.NumberMessage);
  62. return;
  63. }
  64. if (this.IsPhoneNumber && !this.CheckIsPhoneNum())
  65. {
  66. this.ShowError(this.PhoneNumberMessage);
  67. return;
  68. }
  69. this.HideError();
  70. }
  71. #region 控件名称
  72. private Popup PART_ErrorPopup;
  73. private TextBlock PART_ErrorContent;
  74. #endregion
  75. #region 依赖属性
  76. #region 是否必填
  77. public static readonly DependencyProperty IsRequiredProperty = DependencyProperty.Register("IsRequired"
  78. , typeof(bool), typeof(ValidateTextBox));
  79. /// <summary>
  80. /// 是否必填
  81. /// </summary>
  82. public bool IsRequired
  83. {
  84. get { return (bool)GetValue(IsRequiredProperty); }
  85. set { SetValue(IsRequiredProperty, value); }
  86. }
  87. public static readonly DependencyProperty RequiredMessageProperty = DependencyProperty.Register("RequiredMessage"
  88. , typeof(string), typeof(ValidateTextBox));
  89. /// <summary>
  90. /// 是否必填
  91. /// </summary>
  92. public string RequiredMessage
  93. {
  94. get { return (string)GetValue(RequiredMessageProperty); }
  95. set { SetValue(RequiredMessageProperty, value); }
  96. }
  97. #endregion
  98. #region 是否为数字
  99. public static readonly DependencyProperty IsNumberProperty = DependencyProperty.Register("IsNumber"
  100. , typeof(bool), typeof(ValidateTextBox));
  101. /// <summary>
  102. /// 是否为数字
  103. /// </summary>
  104. public bool IsNumber
  105. {
  106. get { return (bool)GetValue(IsNumberProperty); }
  107. set { SetValue(IsNumberProperty, value); }
  108. }
  109. public static readonly DependencyProperty NumberMessageProperty = DependencyProperty.Register("NumberMessage"
  110. , typeof(string), typeof(ValidateTextBox));
  111. /// <summary>
  112. /// 是否为数字
  113. /// </summary>
  114. public string NumberMessage
  115. {
  116. get { return (string)GetValue(NumberMessageProperty); }
  117. set { SetValue(NumberMessageProperty, value); }
  118. }
  119. #endregion
  120. #region 是否为号码(电话号码、手机号码)
  121. public static readonly DependencyProperty IsPhoneNumberProperty = DependencyProperty.Register("IsPhoneNumber"
  122. , typeof(bool), typeof(ValidateTextBox));
  123. /// <summary>
  124. /// 是否为号码(电话号码、手机号码)
  125. /// </summary>
  126. public bool IsPhoneNumber
  127. {
  128. get { return (bool)GetValue(IsPhoneNumberProperty); }
  129. set { SetValue(IsPhoneNumberProperty, value); }
  130. }
  131. public static readonly DependencyProperty PhoneNumberMessageProperty = DependencyProperty.Register("PhoneNumberMessage"
  132. , typeof(string), typeof(ValidateTextBox));
  133. /// <summary>
  134. /// 是否为号码(电话号码、手机号码)的提示信息
  135. /// </summary>
  136. public string PhoneNumberMessage
  137. {
  138. get { return (string)GetValue(PhoneNumberMessageProperty); }
  139. set { SetValue(PhoneNumberMessageProperty, value); }
  140. }
  141. #endregion
  142. #region 是否校验
  143. public static readonly DependencyProperty IsValidateProperty = DependencyProperty.Register("IsValidate"
  144. , typeof(bool), typeof(ValidateTextBox)
  145. , new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsValidateChanged)));
  146. /// <summary>
  147. /// 是否校验
  148. /// </summary>
  149. public bool IsValidate
  150. {
  151. get { return (bool)GetValue(IsValidateProperty); }
  152. set { SetValue(IsValidateProperty, value); }
  153. }
  154. private static void OnIsValidateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  155. {
  156. ValidateTextBox validateTextBox = (ValidateTextBox)sender;
  157. if (e.Property == IsValidateProperty)
  158. {
  159. if (Convert.ToBoolean(e.NewValue))
  160. {
  161. validateTextBox.BeginValidate();
  162. }
  163. }
  164. }
  165. #endregion
  166. public static readonly DependencyProperty ValidateTypeProperty = DependencyProperty.Register("ValidateType"
  167. , typeof(EnumValidateType), typeof(ValidateTextBox));
  168. /// <summary>
  169. ///
  170. /// </summary>
  171. public EnumValidateType ValidateType
  172. {
  173. get { return (EnumValidateType)GetValue(ValidateTypeProperty); }
  174. set { SetValue(ValidateTypeProperty, value); }
  175. }
  176. #endregion
  177. public override void OnApplyTemplate()
  178. {
  179. base.OnApplyTemplate();
  180. this.PART_ErrorPopup = this.GetTemplateChild("PART_ErrorPopup") as Popup;
  181. this.PART_ErrorContent = this.GetTemplateChild("PART_ErrorContent") as TextBlock;
  182. }
  183. #region 校验方法
  184. /// <summary>
  185. /// 显示错误提示信息
  186. /// </summary>
  187. /// <param name="errorContent"></param>
  188. private void ShowError(string errorContent)
  189. {
  190. this.PART_ErrorContent.Text = errorContent;
  191. VisualStateManager.GoToState(this, "InvalidFocused", true);
  192. //this.PART_ErrorPopup.IsOpen = true;
  193. }
  194. /// <summary>
  195. /// 隐藏错误提示信息
  196. /// </summary>
  197. private void HideError()
  198. {
  199. //this.PART_ErrorPopup.IsOpen = false;
  200. VisualStateManager.GoToState(this, "ValidUnfocused", true);
  201. }
  202. /// <summary>
  203. /// 判断是否为空
  204. /// </summary>
  205. /// <returns></returns>
  206. private bool CheckIsEmpty()
  207. {
  208. return string.IsNullOrEmpty(this.Text);
  209. }
  210. /// <summary>
  211. /// 判断是否为数字
  212. /// </summary>
  213. /// <returns></returns>
  214. private bool CheckIsNumber()
  215. {
  216. return Regex.IsMatch(this.Text, @"^[0-9]*$");
  217. }
  218. private bool CheckIsPhoneNum()
  219. {
  220. return Regex.IsMatch(this.Text, @"^[1][358][0-9]{9}$");
  221. }
  222. #endregion
  223. }
  224. public enum EnumValidateType
  225. {
  226. LostFocus,
  227. Loaded,
  228. }
  229. }