WritableComboBoxControl.xaml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Compdfkit_Tools.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace Compdfkit_Tools.Common
  18. {
  19. /// <summary>
  20. /// WritableComboBoxControl.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class WritableComboBoxControl : UserControl
  23. {
  24. public event EventHandler<string> TextChanged;
  25. public string SelectedIndex
  26. {
  27. get { return (string)GetValue(SelectedIndexProperty); }
  28. set
  29. {
  30. SetValue(SelectedIndexProperty, value);
  31. }
  32. }
  33. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  34. public static readonly DependencyProperty SelectedIndexProperty =
  35. DependencyProperty.Register("SelectedIndex", typeof(string), typeof(WritableComboBoxControl), new PropertyMetadata("0"));
  36. public string Text
  37. {
  38. get { return (string)GetValue(TextProperty); }
  39. set { SetValue(TextProperty, value); }
  40. }
  41. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  42. public static readonly DependencyProperty TextProperty =
  43. DependencyProperty.Register("Text", typeof(string), typeof(WritableComboBoxControl), new PropertyMetadata(""));
  44. public int MaxPageIndex
  45. {
  46. get { return (int)GetValue(MaxPageIndexProperty); }
  47. set {
  48. SetValue(MaxPageIndexProperty, value);
  49. UpDataPagesInRange();
  50. }
  51. }
  52. // Using a DependencyProperty as the backing store for MaxPageRange. This enables animation, styling, binding, etc...
  53. public static readonly DependencyProperty MaxPageIndexProperty =
  54. DependencyProperty.Register("MaxPageIndex", typeof(int), typeof(WritableComboBoxControl), new FrameworkPropertyMetadata(0));
  55. private void UpDataPagesInRange()
  56. {
  57. if (ComboBox.SelectedItem == null)
  58. {
  59. return;
  60. }
  61. if (ComboBox.SelectedItem as ComboBoxItem == null)
  62. {
  63. return;
  64. }
  65. if ((ComboBox.SelectedItem as ComboBoxItem).Tag != null)
  66. {
  67. switch ((ComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  68. {
  69. case "AllPages":
  70. Text = "1-" + MaxPageIndex;
  71. TextChanged?.Invoke( null, Text);
  72. break;
  73. case "OddPages":
  74. {
  75. string pageRange = "";
  76. for (int i = 1; i <= MaxPageIndex; i++)
  77. {
  78. if (i % 2 != 0 || MaxPageIndex == 1)
  79. {
  80. if (string.IsNullOrEmpty(pageRange))
  81. {
  82. pageRange = i.ToString();
  83. }
  84. else
  85. {
  86. pageRange += "," + i;
  87. }
  88. }
  89. }
  90. Text = pageRange;
  91. TextChanged?.Invoke(null, Text);
  92. break;
  93. }
  94. case "EvenPages":
  95. {
  96. string pageRange = "";
  97. for (int i = 1; i <= MaxPageIndex; i++)
  98. {
  99. if (i % 2 == 0 || MaxPageIndex == 1)
  100. {
  101. if (string.IsNullOrEmpty(pageRange))
  102. {
  103. pageRange = i.ToString();
  104. }
  105. else
  106. {
  107. pageRange += "," + i;
  108. }
  109. }
  110. }
  111. Text = pageRange;
  112. TextChanged?.Invoke(null, Text);
  113. break;
  114. }
  115. case "CustomPages":
  116. Text = TextBox.Text;
  117. TextChanged?.Invoke(null, Text);
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. }
  124. public WritableComboBoxControl()
  125. {
  126. InitializeComponent();
  127. }
  128. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  129. {
  130. var comboBox = sender as ComboBox;
  131. if (comboBox.SelectedIndex == comboBox.Items.Count - 1)
  132. {
  133. TextBox.Visibility = Visibility.Visible;
  134. }
  135. else
  136. {
  137. TextBox.Visibility = Visibility.Hidden;
  138. }
  139. UpDataPagesInRange();
  140. }
  141. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  142. {
  143. if (e.Key == Key.Enter)
  144. {
  145. ComboBox.Focus();
  146. }
  147. }
  148. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  149. {
  150. e.Handled = new Regex("[^0-9,-]+").IsMatch(e.Text);
  151. }
  152. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  153. {
  154. if (ComboBox.SelectedIndex == ComboBox.Items.Count - 1)
  155. {
  156. Text = TextBox.Text;
  157. }
  158. else { Text = ""; }
  159. TextChanged?.Invoke(null, Text);
  160. }
  161. }
  162. }