NumericUpDownControl.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. namespace Compdfkit_Tools.Common
  7. {
  8. public partial class NumericUpDownControl : UserControl
  9. {
  10. private string regixString = "[^0-9-]";
  11. private string nRegixString = @"[^0-9-]+";
  12. // The dependency property which will be accessible on the UserControl
  13. public static readonly DependencyProperty UnitProperty =
  14. DependencyProperty.Register("Unit", typeof(string), typeof(NumericUpDownControl), new UIPropertyMetadata());
  15. public string Unit
  16. {
  17. get { return (string)GetValue(UnitProperty); }
  18. set { SetValue(UnitProperty, value); }
  19. }
  20. // The dependency property which will be accessible on the UserControl
  21. public static readonly DependencyProperty MaxiumProperty =
  22. DependencyProperty.Register("Maxium", typeof(int), typeof(NumericUpDownControl), new UIPropertyMetadata());
  23. public int Maxium
  24. {
  25. get { return (int)GetValue(MaxiumProperty); }
  26. set { SetValue(MaxiumProperty, value); }
  27. }
  28. // The dependency property which will be accessible on the UserControl
  29. public static readonly DependencyProperty TextProperty =
  30. DependencyProperty.Register("Text", typeof(string), typeof(NumericUpDownControl), new UIPropertyMetadata());
  31. public string Text
  32. {
  33. get { return (string)GetValue(TextProperty); }
  34. set { SetValue(TextProperty, value); }
  35. }
  36. public static readonly DependencyProperty MinimumProperty =
  37. DependencyProperty.Register("Minimum", typeof(int), typeof(NumericUpDownControl), new UIPropertyMetadata());
  38. public int Minimum
  39. {
  40. get { return (int)GetValue(MinimumProperty); }
  41. set { SetValue(MinimumProperty, value); }
  42. }
  43. public NumericUpDownControl()
  44. {
  45. InitializeComponent();
  46. }
  47. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  48. {
  49. if (Minimum > 0)
  50. {
  51. e.Handled = new Regex(regixString).IsMatch(e.Text);
  52. }
  53. else
  54. {
  55. e.Handled = new Regex(nRegixString).IsMatch(e.Text);
  56. }
  57. }
  58. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  59. {
  60. if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
  61. e.Handled = true;
  62. else
  63. e.Handled = false;
  64. }
  65. public static int GetMinAbsoluteValue(int min, int max)
  66. {
  67. if (min <= 0 && max >= 0)
  68. {
  69. return 0;
  70. }
  71. else
  72. {
  73. return Math.Abs(min) < Math.Abs(max) ? min : max;
  74. }
  75. }
  76. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  77. {
  78. if (!string.IsNullOrEmpty(TextBox.Text))
  79. {
  80. if (int.Parse(TextBox.Text) > Maxium)
  81. {
  82. TextBox.Text = Maxium.ToString();
  83. }
  84. if (int.Parse(TextBox.Text) < Minimum)
  85. {
  86. TextBox.Text = Minimum.ToString();
  87. }
  88. }
  89. else
  90. {
  91. TextBox.Text = GetMinAbsoluteValue(Minimum, Maxium).ToString();
  92. }
  93. }
  94. private void UpButton_Click(object sender, RoutedEventArgs e)
  95. {
  96. if (int.TryParse(TextBox.Text, out int num))
  97. {
  98. TextBox.Text = (++num).ToString();
  99. }
  100. }
  101. private void DownButton_Click(object sender, RoutedEventArgs e)
  102. {
  103. if (int.TryParse(TextBox.Text, out int num))
  104. {
  105. TextBox.Text = (--num).ToString();
  106. }
  107. }
  108. }
  109. }