NumericUpDownControl.xaml.cs 3.8 KB

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