DropDownNumberBoxControl.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /// NumberBoxDropDownControl.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class DropDownNumberBoxControl : 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(DropDownNumberBoxControl), 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 TextProperty =
  34. DependencyProperty.Register("Text", typeof(string), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
  35. public string Text
  36. {
  37. get { return (string)GetValue(TextProperty); }
  38. set { SetValue(TextProperty, value); }
  39. }
  40. public static readonly DependencyProperty MaxiumProperty =
  41. DependencyProperty.Register("Maxium", typeof(int), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
  42. public int Maxium
  43. {
  44. get { return (int)GetValue(MaxiumProperty); }
  45. set { SetValue(MaxiumProperty, value); }
  46. }
  47. public static readonly DependencyProperty MinimumProperty =
  48. DependencyProperty.Register("Minimum", typeof(int), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
  49. public int Minimum
  50. {
  51. get { return (int)GetValue(MinimumProperty); }
  52. set { SetValue(MinimumProperty, value); }
  53. }
  54. public event EventHandler<string> InputEnterEvent;
  55. public event EventHandler<string> SetPresetEvent;
  56. public DropDownNumberBoxControl()
  57. {
  58. InitializeComponent();
  59. }
  60. public void InitPresetNumberArray(List<int> presetNumber)
  61. {
  62. List<string> array = new List<string>();
  63. for (int i = 0; i < presetNumber.Count; i++)
  64. {
  65. array.Add(presetNumber[i].ToString());
  66. }
  67. ComboBox.ItemsSource = array;
  68. ComboBox.SelectedIndex = 0;
  69. }
  70. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  71. {
  72. e.Handled = new Regex(regixString).IsMatch(e.Text);
  73. }
  74. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  75. {
  76. if (e.Key == Key.Enter)
  77. {
  78. InputEnterEvent?.Invoke(sender, Text);
  79. }
  80. if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
  81. e.Handled = true;
  82. else
  83. e.Handled = false;
  84. }
  85. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  86. {
  87. if (!string.IsNullOrEmpty(TextBox.Text))
  88. {
  89. if (int.Parse(TextBox.Text) > Maxium)
  90. {
  91. TextBox.Text = Maxium.ToString();
  92. }
  93. if (int.Parse(TextBox.Text) < Minimum)
  94. {
  95. TextBox.Text = Minimum.ToString();
  96. }
  97. }
  98. }
  99. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  100. {
  101. if ((sender as ComboBox).SelectedItem != null)
  102. {
  103. // 处理空引用异常的代码
  104. TextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  105. SetPresetEvent?.Invoke(sender, TextBox.Text);
  106. }
  107. }
  108. }
  109. }