WritableComboBox.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 bool EvenPageIsEnabled
  64. {
  65. get { return (bool)GetValue(EvenPageIsEnabledProperty); }
  66. set { SetValue(EvenPageIsEnabledProperty, value); }
  67. }
  68. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  69. public static readonly DependencyProperty EvenPageIsEnabledProperty =
  70. DependencyProperty.Register("EvenPageIsEnabled", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(true));
  71. public string SelectedIndex
  72. {
  73. get { return (string)GetValue(SelectedIndexProperty); }
  74. set { SetValue(SelectedIndexProperty, value); }
  75. }
  76. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  77. public static readonly DependencyProperty SelectedIndexProperty =
  78. DependencyProperty.Register("SelectedIndex", typeof(string), typeof(WritableComboBox), new PropertyMetadata("0"));
  79. public string Text
  80. {
  81. get { return (string)GetValue(TextProperty); }
  82. set { SetValue(TextProperty, value); }
  83. }
  84. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  85. public static readonly DependencyProperty TextProperty =
  86. DependencyProperty.Register("Text", typeof(string), typeof(WritableComboBox), new PropertyMetadata(""));
  87. private List<int> pageIndexList = new List<int>();
  88. public List<int> PageIndexList
  89. {
  90. private get { return (List<int>)GetValue(PageIndexListProperty); }
  91. set { SetValue(PageIndexListProperty, value); }
  92. }
  93. // Using a DependencyProperty as the backing store for PageIndexList. This enables animation, styling, binding, etc...
  94. public static readonly DependencyProperty PageIndexListProperty =
  95. DependencyProperty.Register("PageIndexList", typeof(List<int>), typeof(WritableComboBox), new PropertyMetadata(new List<int>()));
  96. public int MaxPageRange
  97. {
  98. get { return (int)GetValue(MaxPageRangeProperty); }
  99. set { SetValue(MaxPageRangeProperty, value); }
  100. }
  101. // Using a DependencyProperty as the backing store for MaxPageRange. This enables animation, styling, binding, etc...
  102. public static readonly DependencyProperty MaxPageRangeProperty =
  103. DependencyProperty.Register("MaxPageRange", typeof(int), typeof(WritableComboBox), new PropertyMetadata(0));
  104. public ItemCollection Items
  105. {
  106. get { return (ItemCollection)GetValue(ItemsProperty); }
  107. set { SetValue(ItemsProperty, value); }
  108. }
  109. public static readonly DependencyProperty ItemsProperty =
  110. DependencyProperty.Register("Items", typeof(ItemCollection), typeof(WritableComboBox), new PropertyMetadata());
  111. /// <summary>
  112. /// 把子控件的事件传递出去,方便绑定
  113. /// </summary>
  114. public event RoutedEventHandler SelectionChanged;
  115. /// <summary>
  116. /// 把子控件的事件传递出去,方便绑定
  117. /// </summary>
  118. public event RoutedEventHandler TextChanged;
  119. public WritableComboBox()
  120. {
  121. InitializeComponent();
  122. this.Items = this.writableComboBox.Items;
  123. }
  124. private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  125. {
  126. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  127. {
  128. if (this.writableComboBox.ActualWidth == 0) { this.writableTextBox.Width = 210; this.writableTextBox.Visibility = Visibility.Visible; return; }
  129. this.writableTextBox.Width = this.writableComboBox.ActualWidth - 18;
  130. Trace.WriteLine(this.writableComboBox.ActualWidth);
  131. this.writableTextBox.Visibility = Visibility.Visible;
  132. }
  133. else
  134. {
  135. if (this.writableTextBox != null)
  136. {
  137. this.writableTextBox.Visibility = Visibility.Hidden;
  138. }
  139. }
  140. if (this.writableComboBox.Items.Count == 5)
  141. {
  142. if (this.writableComboBox.SelectedIndex == 1)
  143. { IsCurrentPage = true; }
  144. else
  145. {
  146. IsCurrentPage = false;
  147. }
  148. }
  149. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  150. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  151. {
  152. return;
  153. }
  154. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  155. {
  156. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  157. {
  158. case "AllPage":
  159. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  160. {
  161. PageIndexList = pageIndexList;
  162. Text = "1-" + MaxPageRange;
  163. }
  164. break;
  165. case "OddPage":
  166. {
  167. string pageRange = "";
  168. for (int i = 1; i <= MaxPageRange; i++)
  169. {
  170. if (i % 2 != 0 || MaxPageRange == 1)
  171. {
  172. if (string.IsNullOrEmpty(pageRange))
  173. {
  174. pageRange = i.ToString();
  175. }
  176. else
  177. {
  178. pageRange += "," + i;
  179. }
  180. }
  181. }
  182. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  183. {
  184. PageIndexList = pageIndexList;
  185. Text = pageRange;
  186. }
  187. break;
  188. }
  189. case "EvenPage":
  190. {
  191. string pageRange = "";
  192. for (int i = 1; i <= MaxPageRange; i++)
  193. {
  194. if (i % 2 == 0 || MaxPageRange == 1)
  195. {
  196. if (string.IsNullOrEmpty(pageRange))
  197. {
  198. pageRange = i.ToString();
  199. }
  200. else
  201. {
  202. pageRange += "," + i;
  203. }
  204. }
  205. }
  206. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  207. {
  208. PageIndexList = pageIndexList;
  209. Text = pageRange;
  210. }
  211. break;
  212. }
  213. case "CustomPage":
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. SelectionChanged?.Invoke(sender, e);
  220. }
  221. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  222. {
  223. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  224. {
  225. Text = this.writableTextBox.Text;
  226. }
  227. else { Text = ""; }
  228. TextChanged?.Invoke(sender, e);
  229. }
  230. private void writableTextBox_LostFocus(object sender, RoutedEventArgs e)
  231. {
  232. if (CommonHelper.GetPagesInRange(ref pageIndexList, writableTextBox.Text, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  233. {
  234. PageIndexList = pageIndexList;
  235. Text = writableTextBox.Text;
  236. }
  237. else
  238. {
  239. writableTextBox.Text = "";
  240. }
  241. }
  242. private void writableTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  243. {
  244. if (e.Key == Key.Enter)
  245. {
  246. writableComboBox.Focus();
  247. }
  248. }
  249. }
  250. }