NumericUpDown.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. SetValue(MinimumProperty, value);
  83. }
  84. }
  85. public static readonly DependencyProperty MinimumProperty =
  86. DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
  87. /// <summary>
  88. /// 增长步长
  89. /// </summary>
  90. public double TickFrequency
  91. {
  92. get { return (double)GetValue(TickFrequencyProperty); }
  93. set { SetValue(TickFrequencyProperty, value); }
  94. }
  95. public static readonly DependencyProperty TickFrequencyProperty =
  96. DependencyProperty.Register("TickFrequency", typeof(double), typeof(NumericUpDown), new PropertyMetadata(1.0));
  97. public Visibility ShowBtn
  98. {
  99. get { return (Visibility)GetValue(ShowBtnProperty); }
  100. set { SetValue(ShowBtnProperty, value); }
  101. }
  102. public static readonly DependencyProperty ShowBtnProperty =
  103. DependencyProperty.Register("ShowBtn", typeof(Visibility), typeof(NumericUpDown), new PropertyMetadata(Visibility.Visible));
  104. public NumericUpDown()
  105. {
  106. InitializeComponent();
  107. }
  108. private void Button_Add_Click(object sender, RoutedEventArgs e)
  109. {
  110. int num = 0;
  111. double targetvalue = 0;
  112. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  113. if (result)
  114. {
  115. targetvalue = num + TickFrequency;
  116. }
  117. else
  118. {
  119. targetvalue = Maximum;
  120. }
  121. if (targetvalue > Maximum)
  122. {
  123. targetvalue = Maximum;
  124. }
  125. this.TextBox_Num.Text = targetvalue.ToString();
  126. Text = this.TextBox_Num.Text;
  127. Value = targetvalue;
  128. }
  129. private void Button_Sub_Click(object sender, RoutedEventArgs e)
  130. {
  131. int num = 0;
  132. double targetvalue = 0;
  133. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  134. if (result)
  135. {
  136. targetvalue = num - TickFrequency;
  137. }
  138. else
  139. {
  140. targetvalue = Minimum;
  141. }
  142. if(targetvalue<Minimum)
  143. {
  144. targetvalue = Minimum;
  145. }
  146. this.TextBox_Num.Text = targetvalue.ToString();
  147. Text = this.TextBox_Num.Text;
  148. Value = targetvalue;
  149. }
  150. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  151. {
  152. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  153. }
  154. private void TextBox_Num_LostFocus(object sender, RoutedEventArgs e)
  155. {
  156. //校验输入的过程 放在失去焦点中,而不是放在放在文本变化事件中
  157. //如果放在文本变化事件中 会影响用户正常的输入,体验比较别扭
  158. CheckInput();
  159. }
  160. /// <summary>
  161. /// 校验输入是否合法
  162. /// </summary>
  163. private void CheckInput()
  164. {
  165. //控件未显示前不进行逻辑处理 避免初始化前卡顿
  166. if (!this.IsLoaded)
  167. return;
  168. //int.TryParse("30.0000610351563"),字符串有小数点,会转换失败
  169. double num = 0;
  170. if (/*this.TextBox_Num.Text == "" ||*/ !double.TryParse(this.TextBox_Num.Text, out num))
  171. {
  172. this.TextBox_Num.Text = Minimum.ToString();
  173. }
  174. if (double.Parse(this.TextBox_Num.Text) < Minimum)
  175. {
  176. this.TextBox_Num.Text = Minimum.ToString();
  177. }
  178. Text = this.TextBox_Num.Text;
  179. Value = (int)double.Parse(this.TextBox_Num.Text);
  180. }
  181. private void TextBox_Num_PreviewKeyDown(object sender, KeyEventArgs e)
  182. {
  183. if (e.Key == Key.Enter)
  184. {
  185. CheckInput();
  186. }
  187. }
  188. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  189. {
  190. Text = Minimum.ToString();
  191. }
  192. }
  193. }