NumericUpDownControl.xaml.cs 3.7 KB

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