WritableComboBoxControl.xaml.cs 5.8 KB

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