WritableComboBox.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  155. {
  156. case "AllPage":
  157. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  158. {
  159. PageIndexList = pageIndexList;
  160. Text = "1-" + MaxPageRange;
  161. }
  162. break;
  163. case "OddPage":
  164. {
  165. string pageRange = "";
  166. for (int i = 1; i <= MaxPageRange; i++)
  167. {
  168. if (i % 2 != 0 || MaxPageRange == 1)
  169. {
  170. if (string.IsNullOrEmpty(pageRange))
  171. {
  172. pageRange = i.ToString();
  173. }
  174. else
  175. {
  176. pageRange += "," + i;
  177. }
  178. }
  179. }
  180. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  181. {
  182. PageIndexList = pageIndexList;
  183. Text = pageRange;
  184. }
  185. break;
  186. }
  187. case "EvenPage":
  188. {
  189. string pageRange = "";
  190. for (int i = 1; i <= MaxPageRange; i++)
  191. {
  192. if (i % 2 == 0 || MaxPageRange == 1)
  193. {
  194. if (string.IsNullOrEmpty(pageRange))
  195. {
  196. pageRange = i.ToString();
  197. }
  198. else
  199. {
  200. pageRange += "," + i;
  201. }
  202. }
  203. }
  204. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  205. {
  206. PageIndexList = pageIndexList;
  207. Text = pageRange;
  208. }
  209. break;
  210. }
  211. case "CustomPage":
  212. break;
  213. default:
  214. break;
  215. }
  216. SelectionChanged?.Invoke(sender, e);
  217. }
  218. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  219. {
  220. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  221. {
  222. Text = this.writableTextBox.Text;
  223. }
  224. else { Text = ""; }
  225. TextChanged?.Invoke(sender, e);
  226. }
  227. private void writableTextBox_LostFocus(object sender, RoutedEventArgs e)
  228. {
  229. if (CommonHelper.GetPagesInRange(ref pageIndexList, writableTextBox.Text, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  230. {
  231. PageIndexList = pageIndexList;
  232. Text = writableTextBox.Text;
  233. }
  234. else
  235. {
  236. writableTextBox.Text = "";
  237. }
  238. }
  239. private void writableTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  240. {
  241. if (e.Key == Key.Enter)
  242. {
  243. writableComboBox.Focus();
  244. }
  245. }
  246. }
  247. }