NumericUpDown.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_Office.CustomControl
  17. {
  18. /// <summary>
  19. /// NumericUpDown.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class NumericUpDown : UserControl
  22. {
  23. //public int Num
  24. //{
  25. // get
  26. // {
  27. // int value = 0;
  28. // this.Dispatcher.Invoke(new Action(() =>
  29. // value = Convert.ToInt32(this.TextBox_Num.Text.Trim())
  30. // ));
  31. // return value;
  32. // }
  33. // set
  34. // {
  35. // this.Dispatcher.Invoke(new Action(() =>
  36. // {
  37. // this.TextBox_Num.Text = value.ToString();
  38. // }));
  39. // }
  40. //}
  41. public string Text
  42. {
  43. get { return (string)GetValue(TextProperty); }
  44. set { SetValue(TextProperty, value); }
  45. }
  46. public static readonly DependencyProperty TextProperty =
  47. DependencyProperty.Register("Text", typeof(string), typeof(NumericUpDown), new PropertyMetadata(""));
  48. /// <summary>
  49. /// 当前值
  50. /// </summary>
  51. public double Value
  52. {
  53. get { return (double)GetValue(ValueProperty); }
  54. set {
  55. if (value > Maximum)
  56. {
  57. SetValue(ValueProperty, Maximum);
  58. }
  59. else if (value < Minimum)
  60. {
  61. SetValue(ValueProperty, Minimum);
  62. }
  63. else
  64. {
  65. SetValue(ValueProperty, value);
  66. TextBox_Num.Text = Value.ToString();
  67. }
  68. }
  69. }
  70. public static readonly DependencyProperty ValueProperty =
  71. DependencyProperty.Register("Value", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
  72. /// <summary>
  73. /// 最大值
  74. /// </summary>
  75. public double Maximum
  76. {
  77. get { return (double)GetValue(MaximumProperty); }
  78. set { SetValue(MaximumProperty, value); }
  79. }
  80. public static readonly DependencyProperty MaximumProperty =
  81. DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(9999.0));
  82. /// <summary>
  83. /// 最小值
  84. /// </summary>
  85. public double Minimum
  86. {
  87. get { return (double)GetValue(MinimumProperty); }
  88. set { SetValue(MinimumProperty, value); }
  89. }
  90. public static readonly DependencyProperty MinimumProperty =
  91. DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
  92. /// <summary>
  93. /// 增长步长
  94. /// </summary>
  95. public double TickFrequency
  96. {
  97. get { return (double)GetValue(TickFrequencyProperty); }
  98. set { SetValue(TickFrequencyProperty, value); }
  99. }
  100. public static readonly DependencyProperty TickFrequencyProperty =
  101. DependencyProperty.Register("TickFrequency", typeof(double), typeof(NumericUpDown), new PropertyMetadata(1.0));
  102. public Visibility ShowBtn
  103. {
  104. get { return (Visibility)GetValue(ShowBtnProperty); }
  105. set { SetValue(ShowBtnProperty, value); }
  106. }
  107. public static readonly DependencyProperty ShowBtnProperty =
  108. DependencyProperty.Register("ShowBtn", typeof(Visibility), typeof(NumericUpDown), new PropertyMetadata(Visibility.Visible));
  109. public NumericUpDown()
  110. {
  111. InitializeComponent();
  112. }
  113. private void Button_Add_Click(object sender, RoutedEventArgs e)
  114. {
  115. int num = 0;
  116. double targetvalue = 0;
  117. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  118. if (result)
  119. {
  120. targetvalue = num + TickFrequency;
  121. }
  122. else
  123. {
  124. targetvalue = Maximum;
  125. }
  126. if(targetvalue>Maximum)
  127. {
  128. targetvalue = Maximum;
  129. }
  130. this.TextBox_Num.Text = targetvalue.ToString();
  131. Text = this.TextBox_Num.Text;
  132. Value = targetvalue;
  133. }
  134. private void Button_Sub_Click(object sender, RoutedEventArgs e)
  135. {
  136. int num = 0;
  137. double targetvalue = 0;
  138. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  139. if (result)
  140. {
  141. targetvalue = num - TickFrequency;
  142. }
  143. else
  144. {
  145. targetvalue = Minimum;
  146. }
  147. if(targetvalue<Minimum)
  148. {
  149. targetvalue = Minimum;
  150. }
  151. this.TextBox_Num.Text = targetvalue.ToString();
  152. Text = this.TextBox_Num.Text;
  153. Value = targetvalue;
  154. }
  155. private void TextBox_Num_TextChanged(object sender, TextChangedEventArgs e)
  156. {
  157. int num = 0;
  158. if (this.TextBox_Num.Text == "" || !int.TryParse(this.TextBox_Num.Text,out num))
  159. {
  160. this.TextBox_Num.Text = Minimum.ToString();
  161. }
  162. Text = this.TextBox_Num.Text;
  163. Value = int.Parse(this.TextBox_Num.Text);
  164. }
  165. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  166. {
  167. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  168. }
  169. }
  170. }