DropDownNumberBoxControl.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 event EventHandler<string> TextChangedEvent;
  45. public DropDownNumberBoxControl()
  46. {
  47. InitializeComponent();
  48. }
  49. public void InitPresetNumberArray(List<int> presetNumber)
  50. {
  51. List<string> array = new List<string>();
  52. //array.Add("Actual size");
  53. //array.Add("Suitable width");
  54. //array.Add("Single page size");
  55. for (int i = 0; i < presetNumber.Count; i++)
  56. {
  57. array.Add(presetNumber[i].ToString());
  58. }
  59. ComboBox.ItemsSource = array;
  60. ComboBox.SelectedIndex = -1;
  61. }
  62. public void SelectValueItem(int valueData)
  63. {
  64. try
  65. {
  66. foreach (string checkItem in ComboBox.Items)
  67. {
  68. if(int.TryParse(checkItem, out int itemValue) && valueData== itemValue)
  69. {
  70. ComboBox.SelectedItem = checkItem;
  71. return;
  72. }
  73. }
  74. ComboBox.SelectedIndex= -1;
  75. }
  76. catch(Exception ex)
  77. {
  78. }
  79. }
  80. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  81. {
  82. e.Handled = new Regex(regixString).IsMatch(e.Text);
  83. }
  84. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  85. {
  86. if (e.Key == Key.Enter)
  87. {
  88. InputEnterEvent?.Invoke(sender, Text);
  89. }
  90. if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
  91. e.Handled = true;
  92. else
  93. e.Handled = false;
  94. }
  95. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  96. {
  97. if (!string.IsNullOrEmpty(TextBox.Text))
  98. {
  99. int num;
  100. int.TryParse(TextBox.Text,out num);
  101. if (num > Maxium)
  102. {
  103. TextBox.Text = Maxium.ToString();
  104. }
  105. if (num < Minimum)
  106. {
  107. TextBox.Text = Minimum.ToString();
  108. }
  109. }
  110. TextChangedEvent?.Invoke(this, TextBox.Text);
  111. }
  112. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  113. {
  114. if ((sender as ComboBox).SelectedItem != null)
  115. {
  116. TextBox.Text = (sender as ComboBox).SelectedItem.ToString();
  117. SetPresetEvent?.Invoke(sender, TextBox.Text);
  118. }
  119. }
  120. }
  121. }