NumericUpDown.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace PDF_Master.CustomControl
  17. {
  18. /// <summary>
  19. /// NumericUpDown.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class NumericUpDown : UserControl
  22. {
  23. public string Text
  24. {
  25. get { return (string)GetValue(TextProperty); }
  26. set { SetValue(TextProperty, value); }
  27. }
  28. public static readonly DependencyProperty TextProperty =
  29. DependencyProperty.Register("Text", typeof(string), typeof(NumericUpDown), new PropertyMetadata("1"));
  30. /// <summary>
  31. /// 当前值
  32. /// </summary>
  33. public double Value
  34. {
  35. get { return (double)GetValue(ValueProperty); }
  36. set
  37. {
  38. SetValue(ValueProperty, value);
  39. if (value > Maximum)
  40. {
  41. SetValue(ValueProperty, Maximum);
  42. }
  43. else if (value < Minimum)
  44. {
  45. SetValue(ValueProperty, Minimum);
  46. }
  47. else
  48. {
  49. SetValue(ValueProperty, value);
  50. TextBox_Num.Text = Value.ToString();
  51. }
  52. }
  53. }
  54. public static readonly DependencyProperty ValueProperty =
  55. DependencyProperty.Register("Value", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0, SelectedItemSourcePropertyChanged));
  56. private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  57. {
  58. var control = d as NumericUpDown;
  59. var value = (double)e.NewValue;
  60. if (control != null)
  61. {
  62. control.TextBox_Num.Text = Convert.ToString(value);
  63. }
  64. }
  65. /// <summary>
  66. /// 最大值
  67. /// </summary>
  68. public double Maximum
  69. {
  70. get { return (double)GetValue(MaximumProperty); }
  71. set { SetValue(MaximumProperty, value); }
  72. }
  73. public static readonly DependencyProperty MaximumProperty =
  74. DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(9999.0));
  75. /// <summary>
  76. /// 最小值
  77. /// </summary>
  78. public double Minimum
  79. {
  80. get { return (double)GetValue(MinimumProperty); }
  81. set
  82. {
  83. SetValue(MinimumProperty, value);
  84. }
  85. }
  86. public static readonly DependencyProperty MinimumProperty =
  87. DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
  88. /// <summary>
  89. /// 增长步长
  90. /// </summary>
  91. public double TickFrequency
  92. {
  93. get { return (double)GetValue(TickFrequencyProperty); }
  94. set { SetValue(TickFrequencyProperty, value); }
  95. }
  96. public static readonly DependencyProperty TickFrequencyProperty =
  97. DependencyProperty.Register("TickFrequency", typeof(double), typeof(NumericUpDown), new PropertyMetadata(1.0));
  98. public Visibility ShowBtn
  99. {
  100. get { return (Visibility)GetValue(ShowBtnProperty); }
  101. set { SetValue(ShowBtnProperty, value); }
  102. }
  103. public static readonly DependencyProperty ShowBtnProperty =
  104. DependencyProperty.Register("ShowBtn", typeof(Visibility), typeof(NumericUpDown), new PropertyMetadata(Visibility.Visible));
  105. public NumericUpDown()
  106. {
  107. InitializeComponent();
  108. }
  109. private void Button_Add_Click(object sender, RoutedEventArgs e)
  110. {
  111. int num = 0;
  112. double targetvalue = 0;
  113. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  114. if (result)
  115. {
  116. targetvalue = num + TickFrequency;
  117. }
  118. else
  119. {
  120. targetvalue = Maximum;
  121. }
  122. if (targetvalue > Maximum)
  123. {
  124. targetvalue = Maximum;
  125. }
  126. this.TextBox_Num.Text = targetvalue.ToString();
  127. Text = this.TextBox_Num.Text;
  128. Value = targetvalue;
  129. }
  130. private void Button_Sub_Click(object sender, RoutedEventArgs e)
  131. {
  132. int num = 0;
  133. double targetvalue = 0;
  134. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  135. if (result)
  136. {
  137. targetvalue = num - TickFrequency;
  138. }
  139. else
  140. {
  141. targetvalue = Minimum;
  142. }
  143. if (targetvalue < Minimum)
  144. {
  145. targetvalue = Minimum;
  146. }
  147. this.TextBox_Num.Text = targetvalue.ToString();
  148. Text = this.TextBox_Num.Text;
  149. Value = targetvalue;
  150. }
  151. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  152. {
  153. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  154. }
  155. private void TextBox_Num_LostFocus(object sender, RoutedEventArgs e)
  156. {
  157. //校验输入的过程 放在失去焦点中,而不是放在放在文本变化事件中
  158. //如果放在文本变化事件中 会影响用户正常的输入,体验比较别扭
  159. CheckInput();
  160. }
  161. /// <summary>
  162. /// 校验输入是否合法
  163. /// </summary>
  164. private void CheckInput()
  165. {
  166. //控件未显示前不进行逻辑处理 避免初始化前卡顿
  167. if (!this.IsLoaded)
  168. return;
  169. //int.TryParse("30.0000610351563"),字符串有小数点,会转换失败
  170. double num = 0;
  171. if (/*this.TextBox_Num.Text == "" ||*/ !double.TryParse(this.TextBox_Num.Text, out num))
  172. {
  173. this.TextBox_Num.Text = Minimum.ToString();
  174. }
  175. if (double.Parse(this.TextBox_Num.Text) < Minimum)
  176. {
  177. this.TextBox_Num.Text = Minimum.ToString();
  178. }
  179. Text = this.TextBox_Num.Text;
  180. Value = (int)double.Parse(this.TextBox_Num.Text);
  181. }
  182. private void TextBox_Num_PreviewKeyDown(object sender, KeyEventArgs e)
  183. {
  184. if (e.Key == Key.Enter)
  185. {
  186. CheckInput();
  187. }
  188. }
  189. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  190. {
  191. //修复控件初始化显示固定值的问题
  192. if (Value >= Minimum)
  193. {
  194. Text = Value.ToString();
  195. }
  196. }
  197. }
  198. }