NumericUpDownControl.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. namespace Compdfkit_Tools.Common
  8. {
  9. public partial class NumericUpDownControl : UserControl
  10. {
  11. private string regixString = "[^0-9-]";
  12. private string nRegixString = @"[^0-9-]+";
  13. // The dependency property which will be accessible on the UserControl
  14. public static readonly DependencyProperty UnitProperty =
  15. DependencyProperty.Register(nameof(Unit), typeof(string), typeof(NumericUpDownControl), new UIPropertyMetadata());
  16. public string Unit
  17. {
  18. get => (string)GetValue(UnitProperty);
  19. set => SetValue(UnitProperty, value);
  20. }
  21. // The dependency property which will be accessible on the UserControl
  22. public static readonly DependencyProperty MaximumProperty =
  23. DependencyProperty.Register(nameof(Maximum), typeof(int), typeof(NumericUpDownControl), new UIPropertyMetadata(HandleMaxValueChanged));
  24. public int Maximum
  25. {
  26. get => (int)GetValue(MaximumProperty);
  27. set => SetValue(MaximumProperty, value);
  28. }
  29. // The dependency property which will be accessible on the UserControl
  30. public static readonly DependencyProperty TextProperty =
  31. DependencyProperty.Register(nameof(Text), typeof(string), typeof(NumericUpDownControl), new UIPropertyMetadata());
  32. public string Text
  33. {
  34. get => (string)GetValue(TextProperty);
  35. set => SetValue(TextProperty, value);
  36. }
  37. public static readonly DependencyProperty MinimumProperty =
  38. DependencyProperty.Register(nameof(Minimum), typeof(int), typeof(NumericUpDownControl), new UIPropertyMetadata(HandleMinValueChanged));
  39. public int Minimum
  40. {
  41. get => (int)GetValue(MinimumProperty);
  42. set => SetValue(MinimumProperty, value);
  43. }
  44. // The dependency property which will be accessible on the UserControl
  45. public static readonly DependencyProperty IsValueValidProperty =
  46. DependencyProperty.Register(nameof(IsValueValid), typeof(bool), typeof(NumericUpDownControl), new UIPropertyMetadata());
  47. public bool IsValueValid
  48. {
  49. get => (bool)GetValue(IsValueValidProperty);
  50. set => SetValue(IsValueValidProperty, value);
  51. }
  52. private static void HandleMinValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  53. {
  54. NumericUpDownControl source = (NumericUpDownControl) d;
  55. source.Validator.MinValue = (int) e.NewValue;
  56. }
  57. private static void HandleMaxValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  58. {
  59. NumericUpDownControl source = (NumericUpDownControl) d;
  60. source.Validator.MaxValue = (int) e.NewValue;
  61. }
  62. public NumericUpDownControl()
  63. {
  64. InitializeComponent();
  65. }
  66. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  67. {
  68. if (Minimum > 0)
  69. {
  70. e.Handled = new Regex(regixString).IsMatch(e.Text);
  71. }
  72. else
  73. {
  74. e.Handled = new Regex(nRegixString).IsMatch(e.Text);
  75. }
  76. }
  77. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  78. {
  79. if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
  80. e.Handled = true;
  81. else
  82. e.Handled = false;
  83. }
  84. public static int GetMinAbsoluteValue(int min, int max)
  85. {
  86. if (min <= 0 && max >= 0)
  87. {
  88. return 0;
  89. }
  90. else
  91. {
  92. return Math.Abs(min) < Math.Abs(max) ? min : max;
  93. }
  94. }
  95. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  96. {
  97. if(int.TryParse(TextBox.Text, out int num))
  98. {
  99. if (num <= Maximum && num >= Minimum)
  100. {
  101. IsValueValid = true;
  102. return;
  103. }
  104. }
  105. IsValueValid = false;
  106. }
  107. private void UpButton_Click(object sender, RoutedEventArgs e)
  108. {
  109. if (int.TryParse(TextBox.Text, out int num))
  110. {
  111. int newNum = num + 1;
  112. if (newNum > Maximum)
  113. {
  114. TextBox.Text = Maximum.ToString();
  115. }
  116. else if(newNum < Minimum)
  117. {
  118. TextBox.Text = Minimum.ToString();
  119. }
  120. else
  121. {
  122. TextBox.Text = newNum.ToString();
  123. }
  124. }
  125. else
  126. {
  127. TextBox.Text = Minimum.ToString();
  128. }
  129. }
  130. private void DownButton_Click(object sender, RoutedEventArgs e)
  131. {
  132. if (int.TryParse(TextBox.Text, out int num))
  133. {
  134. int newNum = num - 1;
  135. if (newNum > Maximum)
  136. {
  137. TextBox.Text = Maximum.ToString();
  138. }
  139. else if (newNum < Minimum)
  140. {
  141. TextBox.Text = Minimum.ToString();
  142. }
  143. else
  144. {
  145. TextBox.Text = newNum.ToString();
  146. }
  147. }
  148. else
  149. {
  150. TextBox.Text = Minimum.ToString();
  151. }
  152. }
  153. }
  154. public class CustomValidationRule : ValidationRule
  155. {
  156. public int MaxValue { get; set; }
  157. public int MinValue { get; set; }
  158. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  159. {
  160. if (value == null)
  161. {
  162. return new ValidationResult(false, "Value cannot be empty.");
  163. }
  164. if (int.TryParse(value.ToString(), out int num))
  165. {
  166. if (num <= MaxValue && num >= MinValue)
  167. {
  168. return new ValidationResult(true, null);
  169. }
  170. else
  171. {
  172. return new ValidationResult(false, $"Value must be between {MinValue} and {MaxValue}.");
  173. }
  174. }
  175. else
  176. {
  177. return new ValidationResult(false, "Value must be an integer.");
  178. }
  179. }
  180. }
  181. }