DropDownNumberBoxControl.xaml.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. namespace ComPDFKit.Controls.Common
  8. {
  9. public partial class DropDownNumberBoxControl : UserControl
  10. {
  11. private string regixString = "[^0-9]+";
  12. // The dependency property which will be accessible on the UserControl
  13. public static readonly DependencyProperty UnitProperty =
  14. DependencyProperty.Register("Unit", typeof(string), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
  15. public string Unit
  16. {
  17. get { return (string)GetValue(UnitProperty); }
  18. set { SetValue(UnitProperty, value); }
  19. }
  20. // The dependency property which will be accessible on the UserControl
  21. public static readonly DependencyProperty TextProperty =
  22. DependencyProperty.Register("Text", typeof(string), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
  23. public string Text
  24. {
  25. get { return (string)GetValue(TextProperty); }
  26. set { SetValue(TextProperty, value); }
  27. }
  28. public static readonly DependencyProperty MaxiumProperty =
  29. DependencyProperty.Register("Maxium", typeof(int), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
  30. public int Maxium
  31. {
  32. get { return (int)GetValue(MaxiumProperty); }
  33. set { SetValue(MaxiumProperty, value); }
  34. }
  35. public static readonly DependencyProperty MinimumProperty =
  36. DependencyProperty.Register("Minimum", typeof(int), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
  37. public int Minimum
  38. {
  39. get { return (int)GetValue(MinimumProperty); }
  40. set { SetValue(MinimumProperty, value); }
  41. }
  42. public event EventHandler<string> InputEnterEvent;
  43. public event EventHandler<string> SetPresetEvent;
  44. public DropDownNumberBoxControl()
  45. {
  46. InitializeComponent();
  47. }
  48. public void InitPresetNumberArray(List<int> presetNumber)
  49. {
  50. List<string> array = new List<string>();
  51. //array.Add("Actual size");
  52. //array.Add("Suitable width");
  53. //array.Add("Single page size");
  54. for (int i = 0; i < presetNumber.Count; i++)
  55. {
  56. array.Add(presetNumber[i].ToString());
  57. }
  58. ComboBox.ItemsSource = array;
  59. ComboBox.SelectedIndex = -1;
  60. }
  61. public void SelectValueItem(int valueData)
  62. {
  63. try
  64. {
  65. foreach (string checkItem in ComboBox.Items)
  66. {
  67. if(int.TryParse(checkItem, out int itemValue) && valueData== itemValue)
  68. {
  69. ComboBox.SelectedItem = checkItem;
  70. return;
  71. }
  72. }
  73. ComboBox.SelectedIndex= -1;
  74. }
  75. catch(Exception ex)
  76. {
  77. }
  78. }
  79. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  80. {
  81. e.Handled = new Regex(regixString).IsMatch(e.Text);
  82. }
  83. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  84. {
  85. if (e.Key == Key.Enter)
  86. {
  87. InputEnterEvent?.Invoke(sender, Text);
  88. }
  89. if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
  90. e.Handled = true;
  91. else
  92. e.Handled = false;
  93. }
  94. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  95. {
  96. if (!string.IsNullOrEmpty(TextBox.Text))
  97. {
  98. int num;
  99. int.TryParse(TextBox.Text,out num);
  100. if (num > Maxium)
  101. {
  102. TextBox.Text = Maxium.ToString();
  103. }
  104. if (num < Minimum)
  105. {
  106. TextBox.Text = Minimum.ToString();
  107. }
  108. }
  109. }
  110. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  111. {
  112. if ((sender as ComboBox).SelectedItem != null)
  113. {
  114. TextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  115. SetPresetEvent?.Invoke(sender, TextBox.Text);
  116. }
  117. }
  118. }
  119. }