DropDownNumberBoxControl.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. //array.Add("Actual size");
  64. //array.Add("Suitable width");
  65. //array.Add("Single page size");
  66. for (int i = 0; i < presetNumber.Count; i++)
  67. {
  68. array.Add(presetNumber[i].ToString());
  69. }
  70. ComboBox.ItemsSource = array;
  71. ComboBox.SelectedIndex = -1;
  72. }
  73. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  74. {
  75. e.Handled = new Regex(regixString).IsMatch(e.Text);
  76. }
  77. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  78. {
  79. if (e.Key == Key.Enter)
  80. {
  81. InputEnterEvent?.Invoke(sender, Text);
  82. }
  83. if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
  84. e.Handled = true;
  85. else
  86. e.Handled = false;
  87. }
  88. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  89. {
  90. if (!string.IsNullOrEmpty(TextBox.Text))
  91. {
  92. int num;
  93. int.TryParse(TextBox.Text,out num);
  94. if (num > Maxium)
  95. {
  96. TextBox.Text = Maxium.ToString();
  97. }
  98. if (num < Minimum)
  99. {
  100. TextBox.Text = Minimum.ToString();
  101. }
  102. }
  103. }
  104. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  105. {
  106. if ((sender as ComboBox).SelectedItem != null)
  107. {
  108. // 处理空引用异常的代码
  109. TextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  110. SetPresetEvent?.Invoke(sender, TextBox.Text);
  111. }
  112. }
  113. }
  114. }