WritableComboBox.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using ImTools;
  2. using Newtonsoft.Json.Linq;
  3. using PDF_Office.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace PDF_Office.CustomControl
  20. {
  21. /// <summary>
  22. /// WritableComboBox.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class WritableComboBox : UserControl
  25. {
  26. public bool IsCurrentPage
  27. {
  28. get { return (bool)GetValue(IsCurrentPageProperty); }
  29. set { SetValue(IsCurrentPageProperty, value); }
  30. }
  31. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  32. public static readonly DependencyProperty IsCurrentPageProperty =
  33. DependencyProperty.Register("IsCurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  34. public Visibility IsAllPageVisible
  35. {
  36. get { return (Visibility)GetValue(IsAllPageVisibleProperty); }
  37. set
  38. {
  39. SetValue(IsAllPageVisibleProperty, value);
  40. }
  41. }
  42. // Using a DependencyProperty as the backing store for IsAllPageVisible. This enables animation, styling, binding, etc...
  43. public static readonly DependencyProperty IsAllPageVisibleProperty =
  44. DependencyProperty.Register("IsAllPageVisible", typeof(Visibility), typeof(WritableComboBox), new PropertyMetadata(Visibility.Visible, (d, e) =>
  45. {
  46. if ((Visibility)e.NewValue != Visibility.Visible)
  47. {
  48. (d as WritableComboBox).SetIndexByVisiblity((Visibility)e.NewValue);
  49. }
  50. }));
  51. private void SetIndexByVisiblity(Visibility visible)
  52. {
  53. writableComboBox.SelectedIndex = 1;
  54. }
  55. public bool CurrentPage
  56. {
  57. get { return (bool)GetValue(CurrentPageProperty); }
  58. set { SetValue(CurrentPageProperty, value); }
  59. }
  60. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  61. public static readonly DependencyProperty CurrentPageProperty =
  62. DependencyProperty.Register("CurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  63. public string SelectedIndex
  64. {
  65. get { return (string)GetValue(SelectedIndexProperty); }
  66. set { SetValue(SelectedIndexProperty, value); }
  67. }
  68. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  69. public static readonly DependencyProperty SelectedIndexProperty =
  70. DependencyProperty.Register("SelectedIndex", typeof(string), typeof(WritableComboBox), new PropertyMetadata("0"));
  71. public string Text
  72. {
  73. get { return (string)GetValue(TextProperty); }
  74. set { SetValue(TextProperty, value); }
  75. }
  76. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  77. public static readonly DependencyProperty TextProperty =
  78. DependencyProperty.Register("Text", typeof(string), typeof(WritableComboBox), new PropertyMetadata(""));
  79. private List<int> pageIndexList = new List<int>();
  80. public List<int> PageIndexList
  81. {
  82. private get { return (List<int>)GetValue(PageIndexListProperty); }
  83. set { SetValue(PageIndexListProperty, value); }
  84. }
  85. // Using a DependencyProperty as the backing store for PageIndexList. This enables animation, styling, binding, etc...
  86. public static readonly DependencyProperty PageIndexListProperty =
  87. DependencyProperty.Register("PageIndexList", typeof(List<int>), typeof(WritableComboBox), new PropertyMetadata(new List<int>()));
  88. public int MaxPageRange
  89. {
  90. get { return (int)GetValue(MaxPageRangeProperty); }
  91. set { SetValue(MaxPageRangeProperty, value); }
  92. }
  93. // Using a DependencyProperty as the backing store for MaxPageRange. This enables animation, styling, binding, etc...
  94. public static readonly DependencyProperty MaxPageRangeProperty =
  95. DependencyProperty.Register("MaxPageRange", typeof(int), typeof(WritableComboBox), new PropertyMetadata(0));
  96. public ItemCollection Items
  97. {
  98. get { return (ItemCollection)GetValue(ItemsProperty); }
  99. set { SetValue(ItemsProperty, value); }
  100. }
  101. public static readonly DependencyProperty ItemsProperty =
  102. DependencyProperty.Register("Items", typeof(ItemCollection), typeof(WritableComboBox), new PropertyMetadata());
  103. /// <summary>
  104. /// 把子控件的事件传递出去,方便绑定
  105. /// </summary>
  106. public event RoutedEventHandler SelectionChanged;
  107. /// <summary>
  108. /// 把子控件的事件传递出去,方便绑定
  109. /// </summary>
  110. public event RoutedEventHandler TextChanged;
  111. public WritableComboBox()
  112. {
  113. InitializeComponent();
  114. this.Items = this.writableComboBox.Items;
  115. }
  116. private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  117. {
  118. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  119. {
  120. this.writableTextBox.Width = this.writableComboBox.ActualWidth - 18;
  121. Trace.WriteLine(this.writableComboBox.ActualWidth);
  122. this.writableTextBox.Visibility = Visibility.Visible;
  123. }
  124. else
  125. {
  126. if (this.writableTextBox != null)
  127. {
  128. this.writableTextBox.Visibility = Visibility.Hidden;
  129. }
  130. }
  131. if (this.writableComboBox.Items.Count == 5)
  132. {
  133. if (this.writableComboBox.SelectedIndex == 1)
  134. { IsCurrentPage = true; }
  135. else
  136. {
  137. IsCurrentPage = false;
  138. }
  139. }
  140. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  141. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  142. {
  143. return;
  144. }
  145. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  146. {
  147. case "AllPage":
  148. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  149. {
  150. PageIndexList = pageIndexList;
  151. Text = "1-" + MaxPageRange;
  152. }
  153. break;
  154. case "OddPage":
  155. {
  156. string pageRange = "";
  157. for (int i = 1; i <= MaxPageRange; i++)
  158. {
  159. if (i % 2 != 0 || MaxPageRange == 1)
  160. {
  161. if (string.IsNullOrEmpty(pageRange))
  162. {
  163. pageRange = i.ToString();
  164. }
  165. else
  166. {
  167. pageRange += "," + i;
  168. }
  169. }
  170. }
  171. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  172. {
  173. PageIndexList = pageIndexList;
  174. Text = pageRange;
  175. }
  176. break;
  177. }
  178. case "EvenPage":
  179. {
  180. string pageRange = "";
  181. for (int i = 1; i <= MaxPageRange; i++)
  182. {
  183. if (i % 2 == 0 || MaxPageRange == 1)
  184. {
  185. if (string.IsNullOrEmpty(pageRange))
  186. {
  187. pageRange = i.ToString();
  188. }
  189. else
  190. {
  191. pageRange += "," + i;
  192. }
  193. }
  194. }
  195. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  196. {
  197. PageIndexList = pageIndexList;
  198. Text = pageRange;
  199. }
  200. break;
  201. }
  202. case "CustomPage":
  203. break;
  204. default:
  205. break;
  206. }
  207. SelectionChanged?.Invoke(sender, e);
  208. }
  209. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  210. {
  211. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  212. {
  213. Text = this.writableTextBox.Text;
  214. }
  215. else { Text = ""; }
  216. TextChanged?.Invoke(sender, e);
  217. }
  218. private void writableTextBox_LostFocus(object sender, RoutedEventArgs e)
  219. {
  220. if (CommonHelper.GetPagesInRange(ref pageIndexList, writableTextBox.Text, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  221. {
  222. PageIndexList = pageIndexList;
  223. Text = writableTextBox.Text;
  224. }
  225. else
  226. {
  227. writableTextBox.Text = "";
  228. }
  229. }
  230. private void writableTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  231. {
  232. if (e.Key == Key.Enter)
  233. {
  234. writableComboBox.Focus();
  235. }
  236. }
  237. }
  238. }