NumericUpDownControl.xaml.cs 3.2 KB

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