WritableComboBox.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using ImTools;
  2. using Newtonsoft.Json.Linq;
  3. using PDF_Master.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_Master.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. 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 FrameworkPropertyMetadata(0, new PropertyChangedCallback(OnMaxPageRangeChanged)));
  104. private static void OnMaxPageRangeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  105. {
  106. int value = Convert.ToInt32(e.NewValue);
  107. if (value>0)
  108. {
  109. (d as WritableComboBox).UpDataPagesInRange();
  110. }
  111. }
  112. private void UpDataPagesInRange()
  113. {
  114. if (writableComboBox.SelectedItem==null)
  115. {
  116. return;
  117. }
  118. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  119. {
  120. return;
  121. }
  122. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  123. {
  124. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  125. {
  126. case "AllPage":
  127. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  128. {
  129. PageIndexList = pageIndexList;
  130. Text = "1-" + MaxPageRange;
  131. }
  132. break;
  133. case "OddPage":
  134. {
  135. string pageRange = "";
  136. for (int i = 1; i <= MaxPageRange; i++)
  137. {
  138. if (i % 2 != 0 || MaxPageRange == 1)
  139. {
  140. if (string.IsNullOrEmpty(pageRange))
  141. {
  142. pageRange = i.ToString();
  143. }
  144. else
  145. {
  146. pageRange += "," + i;
  147. }
  148. }
  149. }
  150. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  151. {
  152. PageIndexList = pageIndexList;
  153. Text = pageRange;
  154. }
  155. break;
  156. }
  157. case "EvenPage":
  158. {
  159. string pageRange = "";
  160. for (int i = 1; i <= MaxPageRange; i++)
  161. {
  162. if (i % 2 == 0 || MaxPageRange == 1)
  163. {
  164. if (string.IsNullOrEmpty(pageRange))
  165. {
  166. pageRange = i.ToString();
  167. }
  168. else
  169. {
  170. pageRange += "," + i;
  171. }
  172. }
  173. }
  174. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  175. {
  176. PageIndexList = pageIndexList;
  177. Text = pageRange;
  178. }
  179. break;
  180. }
  181. case "CustomPage":
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. }
  188. public ItemCollection Items
  189. {
  190. get { return (ItemCollection)GetValue(ItemsProperty); }
  191. set { SetValue(ItemsProperty, value); }
  192. }
  193. public static readonly DependencyProperty ItemsProperty =
  194. DependencyProperty.Register("Items", typeof(ItemCollection), typeof(WritableComboBox), new PropertyMetadata());
  195. /// <summary>
  196. /// 把子控件的事件传递出去,方便绑定
  197. /// </summary>
  198. public event RoutedEventHandler SelectionChanged;
  199. /// <summary>
  200. /// 把子控件的事件传递出去,方便绑定
  201. /// </summary>
  202. public event RoutedEventHandler TextChanged;
  203. public WritableComboBox()
  204. {
  205. InitializeComponent();
  206. this.Items = this.writableComboBox.Items;
  207. }
  208. private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  209. {
  210. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  211. {
  212. if (this.writableComboBox.ActualWidth == 0) { this.writableTextBox.Width = 210; this.writableTextBox.Visibility = Visibility.Visible; return; }
  213. this.writableTextBox.Width = this.writableComboBox.ActualWidth - 28;
  214. Trace.WriteLine(this.writableComboBox.ActualWidth);
  215. this.writableTextBox.Visibility = Visibility.Visible;
  216. }
  217. else
  218. {
  219. if (this.writableTextBox != null)
  220. {
  221. this.writableTextBox.Visibility = Visibility.Hidden;
  222. }
  223. }
  224. if (this.writableComboBox.Items.Count == 5)
  225. {
  226. if (this.writableComboBox.SelectedIndex == 1)
  227. { IsCurrentPage = true; }
  228. else
  229. {
  230. IsCurrentPage = false;
  231. }
  232. }
  233. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  234. if (writableComboBox.SelectedItem == null)
  235. {
  236. return;
  237. }
  238. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  239. {
  240. return;
  241. }
  242. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  243. {
  244. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  245. {
  246. case "AllPage":
  247. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  248. {
  249. PageIndexList = pageIndexList;
  250. Text = "1-" + MaxPageRange;
  251. }
  252. break;
  253. case "OddPage":
  254. {
  255. string pageRange = "";
  256. for (int i = 1; i <= MaxPageRange; i++)
  257. {
  258. if (i % 2 != 0 || MaxPageRange == 1)
  259. {
  260. if (string.IsNullOrEmpty(pageRange))
  261. {
  262. pageRange = i.ToString();
  263. }
  264. else
  265. {
  266. pageRange += "," + i;
  267. }
  268. }
  269. }
  270. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  271. {
  272. PageIndexList = pageIndexList;
  273. Text = pageRange;
  274. }
  275. break;
  276. }
  277. case "EvenPage":
  278. {
  279. string pageRange = "";
  280. for (int i = 1; i <= MaxPageRange; i++)
  281. {
  282. if (i % 2 == 0 || MaxPageRange == 1)
  283. {
  284. if (string.IsNullOrEmpty(pageRange))
  285. {
  286. pageRange = i.ToString();
  287. }
  288. else
  289. {
  290. pageRange += "," + i;
  291. }
  292. }
  293. }
  294. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  295. {
  296. PageIndexList = pageIndexList;
  297. Text = pageRange;
  298. }
  299. break;
  300. }
  301. case "CustomPage":
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. SelectionChanged?.Invoke(sender, e);
  308. }
  309. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  310. {
  311. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  312. {
  313. Text = this.writableTextBox.Text;
  314. }
  315. else { Text = ""; }
  316. TextChanged?.Invoke(sender, e);
  317. }
  318. private void writableTextBox_LostFocus(object sender, RoutedEventArgs e)
  319. {
  320. if (CommonHelper.GetPagesInRange(ref pageIndexList, writableTextBox.Text, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  321. {
  322. PageIndexList = pageIndexList;
  323. Text = writableTextBox.Text;
  324. }
  325. else
  326. {
  327. writableTextBox.Text = "";
  328. }
  329. }
  330. private void writableTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  331. {
  332. if (e.Key == Key.Enter)
  333. {
  334. writableComboBox.Focus();
  335. }
  336. }
  337. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  338. {
  339. UpDataPagesInRange();
  340. AllPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_AllPage");
  341. OddPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_OddPage");
  342. EvenPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_EvenPage");
  343. CustomPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_CustomRange");
  344. }
  345. }
  346. }