DropDownNumberBoxControl.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. public void SelectValueItem(int valueData)
  74. {
  75. try
  76. {
  77. foreach (string checkItem in ComboBox.Items)
  78. {
  79. if(int.TryParse(checkItem, out int itemValue) && valueData== itemValue)
  80. {
  81. ComboBox.SelectedItem = checkItem;
  82. return;
  83. }
  84. }
  85. ComboBox.SelectedIndex= -1;
  86. }
  87. catch(Exception ex)
  88. {
  89. }
  90. }
  91. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  92. {
  93. e.Handled = new Regex(regixString).IsMatch(e.Text);
  94. }
  95. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  96. {
  97. if (e.Key == Key.Enter)
  98. {
  99. InputEnterEvent?.Invoke(sender, Text);
  100. }
  101. if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
  102. e.Handled = true;
  103. else
  104. e.Handled = false;
  105. }
  106. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  107. {
  108. if (!string.IsNullOrEmpty(TextBox.Text))
  109. {
  110. int num;
  111. int.TryParse(TextBox.Text,out num);
  112. if (num > Maxium)
  113. {
  114. TextBox.Text = Maxium.ToString();
  115. }
  116. if (num < Minimum)
  117. {
  118. TextBox.Text = Minimum.ToString();
  119. }
  120. }
  121. }
  122. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  123. {
  124. if ((sender as ComboBox).SelectedItem != null)
  125. {
  126. // 处理空引用异常的代码
  127. TextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  128. SetPresetEvent?.Invoke(sender, TextBox.Text);
  129. }
  130. }
  131. }
  132. }