NumericUpDown.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 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. if (value > Maximum)
  39. {
  40. SetValue(ValueProperty, Maximum);
  41. }
  42. else if (value < Minimum)
  43. {
  44. SetValue(ValueProperty, Minimum);
  45. }
  46. else
  47. {
  48. SetValue(ValueProperty, value);
  49. TextBox_Num.Text = Value.ToString();
  50. }
  51. }
  52. }
  53. public static readonly DependencyProperty ValueProperty =
  54. DependencyProperty.Register("Value", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
  55. /// <summary>
  56. /// 最大值
  57. /// </summary>
  58. public double Maximum
  59. {
  60. get { return (double)GetValue(MaximumProperty); }
  61. set { SetValue(MaximumProperty, value); }
  62. }
  63. public static readonly DependencyProperty MaximumProperty =
  64. DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(9999.0));
  65. /// <summary>
  66. /// 最小值
  67. /// </summary>
  68. public double Minimum
  69. {
  70. get { return (double)GetValue(MinimumProperty); }
  71. set { SetValue(MinimumProperty, value); }
  72. }
  73. public static readonly DependencyProperty MinimumProperty =
  74. DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
  75. /// <summary>
  76. /// 增长步长
  77. /// </summary>
  78. public double TickFrequency
  79. {
  80. get { return (double)GetValue(TickFrequencyProperty); }
  81. set { SetValue(TickFrequencyProperty, value); }
  82. }
  83. public static readonly DependencyProperty TickFrequencyProperty =
  84. DependencyProperty.Register("TickFrequency", typeof(double), typeof(NumericUpDown), new PropertyMetadata(1.0));
  85. public Visibility ShowBtn
  86. {
  87. get { return (Visibility)GetValue(ShowBtnProperty); }
  88. set { SetValue(ShowBtnProperty, value); }
  89. }
  90. public static readonly DependencyProperty ShowBtnProperty =
  91. DependencyProperty.Register("ShowBtn", typeof(Visibility), typeof(NumericUpDown), new PropertyMetadata(Visibility.Visible));
  92. public NumericUpDown()
  93. {
  94. InitializeComponent();
  95. }
  96. private void Button_Add_Click(object sender, RoutedEventArgs e)
  97. {
  98. int num = 0;
  99. double targetvalue = 0;
  100. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  101. if (result)
  102. {
  103. targetvalue = num + TickFrequency;
  104. }
  105. else
  106. {
  107. targetvalue = Maximum;
  108. }
  109. if (targetvalue > Maximum)
  110. {
  111. targetvalue = Maximum;
  112. }
  113. this.TextBox_Num.Text = targetvalue.ToString();
  114. Text = this.TextBox_Num.Text;
  115. Value = targetvalue;
  116. }
  117. private void Button_Sub_Click(object sender, RoutedEventArgs e)
  118. {
  119. int num = 0;
  120. double targetvalue = 0;
  121. bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
  122. if (result)
  123. {
  124. targetvalue = num - TickFrequency;
  125. }
  126. else
  127. {
  128. targetvalue = Minimum;
  129. }
  130. if(targetvalue<Minimum)
  131. {
  132. targetvalue = Minimum;
  133. }
  134. this.TextBox_Num.Text = targetvalue.ToString();
  135. Text = this.TextBox_Num.Text;
  136. Value = targetvalue;
  137. }
  138. private void TextBox_Num_TextChanged(object sender, TextChangedEventArgs e)
  139. {
  140. int num = 0;
  141. if (this.TextBox_Num.Text == "" || !int.TryParse(this.TextBox_Num.Text,out num))
  142. {
  143. this.TextBox_Num.Text = Minimum.ToString();
  144. }
  145. if (int.Parse(this.TextBox_Num.Text) < Minimum)
  146. {
  147. this.TextBox_Num.Text = Minimum.ToString();
  148. }
  149. Text = this.TextBox_Num.Text;
  150. Value = int.Parse(this.TextBox_Num.Text);
  151. }
  152. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  153. {
  154. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  155. }
  156. }
  157. }