NumericUpDown.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 { SetValue(MinimumProperty, value); }
  82. }
  83. public static readonly DependencyProperty MinimumProperty =
  84. DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
  85. /// <summary>
  86. /// 增长步长
  87. /// </summary>
  88. public double TickFrequency
  89. {
  90. get { return (double)GetValue(TickFrequencyProperty); }
  91. set { SetValue(TickFrequencyProperty, value); }
  92. }
  93. public static readonly DependencyProperty TickFrequencyProperty =
  94. DependencyProperty.Register("TickFrequency", typeof(double), typeof(NumericUpDown), new PropertyMetadata(1.0));
  95. public Visibility ShowBtn
  96. {
  97. get { return (Visibility)GetValue(ShowBtnProperty); }
  98. set { SetValue(ShowBtnProperty, value); }
  99. }
  100. public static readonly DependencyProperty ShowBtnProperty =
  101. DependencyProperty.Register("ShowBtn", typeof(Visibility), typeof(NumericUpDown), new PropertyMetadata(Visibility.Visible));
  102. public NumericUpDown()
  103. {
  104. InitializeComponent();
  105. }
  106. private void Button_Add_Click(object sender, RoutedEventArgs e)
  107. {
  108. int num = 0;
  109. double targetvalue = 0;
  110. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  111. if (result)
  112. {
  113. targetvalue = num + TickFrequency;
  114. }
  115. else
  116. {
  117. targetvalue = Maximum;
  118. }
  119. if (targetvalue > Maximum)
  120. {
  121. targetvalue = Maximum;
  122. }
  123. this.TextBox_Num.Text = targetvalue.ToString();
  124. Text = this.TextBox_Num.Text;
  125. Value = targetvalue;
  126. }
  127. private void Button_Sub_Click(object sender, RoutedEventArgs e)
  128. {
  129. int num = 0;
  130. double targetvalue = 0;
  131. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  132. if (result)
  133. {
  134. targetvalue = num - TickFrequency;
  135. }
  136. else
  137. {
  138. targetvalue = Minimum;
  139. }
  140. if(targetvalue<Minimum)
  141. {
  142. targetvalue = Minimum;
  143. }
  144. this.TextBox_Num.Text = targetvalue.ToString();
  145. Text = this.TextBox_Num.Text;
  146. Value = targetvalue;
  147. }
  148. private void TextBox_Num_TextChanged(object sender, TextChangedEventArgs e)
  149. {
  150. //控件未显示前不进行逻辑处理 避免初始化前卡顿
  151. if (!this.IsLoaded)
  152. return;
  153. //int.TryParse("30.0000610351563"),字符串有小数点,会转换失败
  154. double num = 0;
  155. if (this.TextBox_Num.Text == "" || !double.TryParse(this.TextBox_Num.Text,out num))
  156. {
  157. this.TextBox_Num.Text = Minimum.ToString();
  158. }
  159. if (double.Parse(this.TextBox_Num.Text) < Minimum)
  160. {
  161. this.TextBox_Num.Text = Minimum.ToString();
  162. }
  163. Text = this.TextBox_Num.Text;
  164. Value = (int)double.Parse(this.TextBox_Num.Text);
  165. }
  166. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  167. {
  168. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  169. }
  170. }
  171. }