NumericUpDownControl.xaml.cs 3.4 KB

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