NumbericTextBox.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using CustomUI.MyControls.Primitives;
  2. using System.Text.RegularExpressions;
  3. using System.Windows;
  4. using System.Windows.Media;
  5. namespace CustomUI
  6. {
  7. /// <summary>
  8. /// 数字输入框
  9. /// </summary>
  10. /// <remarks>add by zhidanfeng 2017.8.19</remarks>
  11. public class NumbericTextBox : ZTextBoxBase
  12. {
  13. #region private fields
  14. private UIElement PART_ErrorPath;
  15. #endregion
  16. #region DependencyProperty
  17. #region Pattern
  18. public string Pattern
  19. {
  20. get { return (string)GetValue(PatternProperty); }
  21. set { SetValue(PatternProperty, value); }
  22. }
  23. public static readonly DependencyProperty PatternProperty =
  24. DependencyProperty.Register("Pattern", typeof(string), typeof(NumbericTextBox), new PropertyMetadata(string.Empty));
  25. #endregion
  26. #region ErrorContent
  27. public string ErrorContent
  28. {
  29. get { return (string)GetValue(ErrorContentProperty); }
  30. set { SetValue(ErrorContentProperty, value); }
  31. }
  32. public static readonly DependencyProperty ErrorContentProperty =
  33. DependencyProperty.Register("ErrorContent", typeof(string), typeof(NumbericTextBox), new PropertyMetadata(string.Empty));
  34. #endregion
  35. #region PatternType
  36. public EnumPatternType PatternType
  37. {
  38. get { return (EnumPatternType)GetValue(PatternTypeProperty); }
  39. set { SetValue(PatternTypeProperty, value); }
  40. }
  41. public static readonly DependencyProperty PatternTypeProperty =
  42. DependencyProperty.Register("PatternType", typeof(EnumPatternType), typeof(NumbericTextBox), new PropertyMetadata(EnumPatternType.None, PatternTypeCallback));
  43. private static void PatternTypeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  44. {
  45. NumbericTextBox textBox = d as NumbericTextBox;
  46. SetPatternAndTip((EnumPatternType)e.NewValue, textBox, false);
  47. }
  48. #endregion
  49. #region ValidateTrigger
  50. public EnumValidateTrigger ValidateTrigger
  51. {
  52. get { return (EnumValidateTrigger)GetValue(ValidateTriggerProperty); }
  53. set { SetValue(ValidateTriggerProperty, value); }
  54. }
  55. public static readonly DependencyProperty ValidateTriggerProperty =
  56. DependencyProperty.Register("ValidateTrigger", typeof(EnumValidateTrigger), typeof(NumbericTextBox), new PropertyMetadata(EnumValidateTrigger.PropertyChanged, ValidateTriggerCallback));
  57. private static void ValidateTriggerCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  58. {
  59. NumbericTextBox textBox = d as NumbericTextBox;
  60. switch ((EnumValidateTrigger)e.NewValue)
  61. {
  62. case EnumValidateTrigger.PropertyChanged:
  63. textBox.LostFocus -= textBox.NumbericTextBox_LostFocus;
  64. textBox.TextChanged += textBox.NumbericTextBox_TextChanged;
  65. break;
  66. case EnumValidateTrigger.LostFocus:
  67. textBox.LostFocus += textBox.NumbericTextBox_LostFocus;
  68. textBox.TextChanged -= textBox.NumbericTextBox_TextChanged;
  69. break;
  70. }
  71. }
  72. #endregion
  73. #region IsHasError
  74. public bool IsHasError
  75. {
  76. get { return (bool)GetValue(IsHasErrorProperty); }
  77. private set { SetValue(IsHasErrorProperty, value); }
  78. }
  79. public static readonly DependencyProperty IsHasErrorProperty =
  80. DependencyProperty.Register("IsHasError", typeof(bool), typeof(NumbericTextBox), new PropertyMetadata(false));
  81. #endregion
  82. #region IsShowErrorTip
  83. /// <summary>
  84. /// 获取或者设置是否显示错误提示文本
  85. /// </summary>
  86. public bool IsShowErrorTip
  87. {
  88. get { return (bool)GetValue(IsShowErrorTipProperty); }
  89. set { SetValue(IsShowErrorTipProperty, value); }
  90. }
  91. public static readonly DependencyProperty IsShowErrorTipProperty =
  92. DependencyProperty.Register("IsShowErrorTip", typeof(bool), typeof(NumbericTextBox), new PropertyMetadata(false));
  93. #endregion
  94. #region ErrorContentTemplate
  95. public DataTemplate ErrorContentTemplate
  96. {
  97. get { return (DataTemplate)GetValue(ErrorContentTemplateProperty); }
  98. set { SetValue(ErrorContentTemplateProperty, value); }
  99. }
  100. public static readonly DependencyProperty ErrorContentTemplateProperty =
  101. DependencyProperty.Register("ErrorContentTemplate", typeof(DataTemplate), typeof(NumbericTextBox));
  102. #endregion
  103. #region ErrorBackground
  104. public Brush ErrorBackground
  105. {
  106. get { return (Brush)GetValue(ErrorBackgroundProperty); }
  107. set { SetValue(ErrorBackgroundProperty, value); }
  108. }
  109. public static readonly DependencyProperty ErrorBackgroundProperty =
  110. DependencyProperty.Register("ErrorBackground", typeof(Brush), typeof(NumbericTextBox));
  111. #endregion
  112. #region IsStartValidate
  113. public bool IsStartValidate
  114. {
  115. get { return (bool)GetValue(IsStartValidateProperty); }
  116. set { SetValue(IsStartValidateProperty, value); }
  117. }
  118. public static readonly DependencyProperty IsStartValidateProperty =
  119. DependencyProperty.Register("IsStartValidate", typeof(bool), typeof(NumbericTextBox), new PropertyMetadata(false, IsStartValidateCallback));
  120. private static void IsStartValidateCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  121. {
  122. NumbericTextBox textBox = d as NumbericTextBox;
  123. if((bool)e.NewValue)
  124. {
  125. textBox.ValidateTextBox();
  126. }
  127. }
  128. #endregion
  129. #endregion
  130. #region Constructors
  131. static NumbericTextBox()
  132. {
  133. DefaultStyleKeyProperty.OverrideMetadata(typeof(NumbericTextBox), new FrameworkPropertyMetadata(typeof(NumbericTextBox)));
  134. }
  135. #endregion
  136. #region Override
  137. public override void OnApplyTemplate()
  138. {
  139. base.OnApplyTemplate();
  140. switch (this.ValidateTrigger)
  141. {
  142. case EnumValidateTrigger.PropertyChanged:
  143. this.TextChanged += NumbericTextBox_TextChanged;
  144. this.LostFocus -= NumbericTextBox_LostFocus;
  145. break;
  146. case EnumValidateTrigger.LostFocus:
  147. this.TextChanged -= NumbericTextBox_TextChanged;
  148. this.LostFocus += NumbericTextBox_LostFocus;
  149. break;
  150. }
  151. //this.PART_ErrorPath = this.GetTemplateChild("PART_ErrorPath") as UIElement;
  152. //if(this.PART_ErrorPath != null)
  153. //{
  154. // this.PART_ErrorPath.MouseEnter += (o, e) =>
  155. // {
  156. // this.IsShowErrorTip = true;
  157. // };
  158. // this.PART_ErrorPath.MouseLeave += (o, e) =>
  159. // {
  160. // this.IsShowErrorTip = false;
  161. // };
  162. //}
  163. NumbericTextBox.SetPatternAndTip(this.PatternType, this, true);
  164. }
  165. private void NumbericTextBox_LostFocus(object sender, RoutedEventArgs e)
  166. {
  167. this.ValidateTextBox();
  168. }
  169. private void NumbericTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
  170. {
  171. this.ValidateTextBox();
  172. }
  173. #endregion
  174. #region private function
  175. private void ValidateTextBox()
  176. {
  177. if (!string.IsNullOrWhiteSpace(this.Pattern))
  178. {
  179. Regex regex = new Regex(this.Pattern);
  180. if (!regex.IsMatch(this.Text))
  181. {
  182. VisualStateManager.GoToState(this, "InputError", true);
  183. this.IsHasError = true;
  184. this.IsShowErrorTip = !string.IsNullOrWhiteSpace(this.ErrorContent);
  185. }
  186. else
  187. {
  188. VisualStateManager.GoToState(this, "Normal", true);
  189. this.IsHasError = false;
  190. this.IsShowErrorTip = false;
  191. }
  192. }
  193. }
  194. private static void SetPatternAndTip(EnumPatternType patternType, NumbericTextBox textBox, bool isFirstLoad)
  195. {
  196. switch (patternType)
  197. {
  198. case EnumPatternType.NotEmpty:
  199. textBox.Pattern = "\\S";
  200. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "不能为空" : textBox.ErrorContent;
  201. break;
  202. case EnumPatternType.OnlyNumber:
  203. textBox.Pattern = "^[0-9]*$";
  204. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "只能输入数字" : textBox.ErrorContent;
  205. break;
  206. case EnumPatternType.IPV4:
  207. 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";
  208. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "IP地址不正确,请输入正确的IPV4地址" : textBox.ErrorContent;
  209. break;
  210. case EnumPatternType.IPV6:
  211. 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]))";
  212. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "IP地址不正确,请输入正确的IPV6地址" : textBox.ErrorContent;
  213. break;
  214. case EnumPatternType.Email:
  215. textBox.Pattern = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
  216. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "邮件地址不正确" : textBox.ErrorContent;
  217. break;
  218. case EnumPatternType.IdCard15:
  219. textBox.Pattern = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
  220. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "身份证格式不正确" : textBox.ErrorContent;
  221. break;
  222. case EnumPatternType.IdCard18:
  223. 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)$";
  224. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "身份证格式不正确" : textBox.ErrorContent;
  225. break;
  226. case EnumPatternType.MobilePhone:
  227. textBox.Pattern = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
  228. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "手机号码输入不正确" : textBox.ErrorContent;
  229. break;
  230. case EnumPatternType.Telephone:
  231. textBox.Pattern = "d{3}-d{8}|d{4}-d{7}";
  232. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "电话号码输入不正确" : textBox.ErrorContent;
  233. break;
  234. case EnumPatternType.OnlyChinese:
  235. textBox.Pattern = "^[\\u4e00-\\u9fa5]{0,}$";
  236. textBox.ErrorContent = string.IsNullOrWhiteSpace(textBox.ErrorContent) ? "只能输入中文" : textBox.ErrorContent;
  237. break;
  238. }
  239. }
  240. #endregion
  241. #region Event Implement Function
  242. #endregion
  243. }
  244. }