WritableComboBox.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. namespace PDF_Master.CustomControl
  21. {
  22. /// <summary>
  23. /// WritableComboBox.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class WritableComboBox : UserControl
  26. {
  27. public bool IsCurrentPage
  28. {
  29. get { return (bool)GetValue(IsCurrentPageProperty); }
  30. set { SetValue(IsCurrentPageProperty, value); }
  31. }
  32. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  33. public static readonly DependencyProperty IsCurrentPageProperty =
  34. DependencyProperty.Register("IsCurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  35. public Visibility IsAllPageVisible
  36. {
  37. get { return (Visibility)GetValue(IsAllPageVisibleProperty); }
  38. set
  39. {
  40. SetValue(IsAllPageVisibleProperty, value);
  41. }
  42. }
  43. // Using a DependencyProperty as the backing store for IsAllPageVisible. This enables animation, styling, binding, etc...
  44. public static readonly DependencyProperty IsAllPageVisibleProperty =
  45. DependencyProperty.Register("IsAllPageVisible", typeof(Visibility), typeof(WritableComboBox), new PropertyMetadata(Visibility.Visible, (d, e) =>
  46. {
  47. if ((Visibility)e.NewValue != Visibility.Visible)
  48. {
  49. (d as WritableComboBox).SetIndexByVisiblity((Visibility)e.NewValue);
  50. }
  51. }));
  52. private void SetIndexByVisiblity(Visibility visible)
  53. {
  54. writableComboBox.SelectedIndex = 1;
  55. }
  56. public bool CurrentPage
  57. {
  58. get { return (bool)GetValue(CurrentPageProperty); }
  59. set { SetValue(CurrentPageProperty, value); }
  60. }
  61. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  62. public static readonly DependencyProperty CurrentPageProperty =
  63. DependencyProperty.Register("CurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  64. public bool EvenPageIsEnabled
  65. {
  66. get { return (bool)GetValue(EvenPageIsEnabledProperty); }
  67. set { SetValue(EvenPageIsEnabledProperty, value); }
  68. }
  69. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  70. public static readonly DependencyProperty EvenPageIsEnabledProperty =
  71. DependencyProperty.Register("EvenPageIsEnabled", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(true));
  72. public string SelectedIndex
  73. {
  74. get { return (string)GetValue(SelectedIndexProperty); }
  75. set { SetValue(SelectedIndexProperty, value); }
  76. }
  77. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  78. public static readonly DependencyProperty SelectedIndexProperty =
  79. DependencyProperty.Register("SelectedIndex", typeof(string), typeof(WritableComboBox), new PropertyMetadata("0"));
  80. public string Text
  81. {
  82. get { return (string)GetValue(TextProperty); }
  83. set { SetValue(TextProperty, value); }
  84. }
  85. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  86. public static readonly DependencyProperty TextProperty =
  87. DependencyProperty.Register("Text", typeof(string), typeof(WritableComboBox), new PropertyMetadata(""));
  88. private List<int> pageIndexList = new List<int>();
  89. public List<int> PageIndexList
  90. {
  91. get { return (List<int>)GetValue(PageIndexListProperty); }
  92. set { SetValue(PageIndexListProperty, value); }
  93. }
  94. // Using a DependencyProperty as the backing store for PageIndexList. This enables animation, styling, binding, etc...
  95. public static readonly DependencyProperty PageIndexListProperty =
  96. DependencyProperty.Register("PageIndexList", typeof(List<int>), typeof(WritableComboBox), new PropertyMetadata(new List<int>()));
  97. public int MaxPageRange
  98. {
  99. get { return (int)GetValue(MaxPageRangeProperty); }
  100. set { SetValue(MaxPageRangeProperty, value); }
  101. }
  102. // Using a DependencyProperty as the backing store for MaxPageRange. This enables animation, styling, binding, etc...
  103. public static readonly DependencyProperty MaxPageRangeProperty =
  104. DependencyProperty.Register("MaxPageRange", typeof(int), typeof(WritableComboBox), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(OnMaxPageRangeChanged)));
  105. private static void OnMaxPageRangeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  106. {
  107. int value = Convert.ToInt32(e.NewValue);
  108. if (value>0)
  109. {
  110. (d as WritableComboBox).UpDataPagesInRange();
  111. }
  112. }
  113. private void UpDataPagesInRange()
  114. {
  115. if (writableComboBox.SelectedItem==null)
  116. {
  117. return;
  118. }
  119. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  120. {
  121. return;
  122. }
  123. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  124. {
  125. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  126. {
  127. case "AllPage":
  128. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  129. {
  130. PageIndexList = pageIndexList;
  131. Text = "1-" + MaxPageRange;
  132. }
  133. break;
  134. case "OddPage":
  135. {
  136. string pageRange = "";
  137. for (int i = 1; i <= MaxPageRange; i++)
  138. {
  139. if (i % 2 != 0 || MaxPageRange == 1)
  140. {
  141. if (string.IsNullOrEmpty(pageRange))
  142. {
  143. pageRange = i.ToString();
  144. }
  145. else
  146. {
  147. pageRange += "," + i;
  148. }
  149. }
  150. }
  151. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  152. {
  153. PageIndexList = pageIndexList;
  154. Text = pageRange;
  155. }
  156. break;
  157. }
  158. case "EvenPage":
  159. {
  160. string pageRange = "";
  161. for (int i = 1; i <= MaxPageRange; i++)
  162. {
  163. if (i % 2 == 0 || MaxPageRange == 1)
  164. {
  165. if (string.IsNullOrEmpty(pageRange))
  166. {
  167. pageRange = i.ToString();
  168. }
  169. else
  170. {
  171. pageRange += "," + i;
  172. }
  173. }
  174. }
  175. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  176. {
  177. PageIndexList = pageIndexList;
  178. Text = pageRange;
  179. }
  180. break;
  181. }
  182. case "CustomPage":
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. }
  189. public ItemCollection Items
  190. {
  191. get { return (ItemCollection)GetValue(ItemsProperty); }
  192. set { SetValue(ItemsProperty, value); }
  193. }
  194. public static readonly DependencyProperty ItemsProperty =
  195. DependencyProperty.Register("Items", typeof(ItemCollection), typeof(WritableComboBox), new PropertyMetadata());
  196. /// <summary>
  197. /// 把子控件的事件传递出去,方便绑定
  198. /// </summary>
  199. public event RoutedEventHandler SelectionChanged;
  200. /// <summary>
  201. /// 把子控件的事件传递出去,方便绑定
  202. /// </summary>
  203. public event RoutedEventHandler TextChanged;
  204. public WritableComboBox()
  205. {
  206. InitializeComponent();
  207. this.Items = this.writableComboBox.Items;
  208. }
  209. private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  210. {
  211. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  212. {
  213. if (this.writableComboBox.ActualWidth == 0) { this.writableTextBox.Width = 210; this.writableTextBox.Visibility = Visibility.Visible; return; }
  214. this.writableTextBox.Width = this.writableComboBox.ActualWidth - 28;
  215. Trace.WriteLine(this.writableComboBox.ActualWidth);
  216. this.writableTextBox.Visibility = Visibility.Visible;
  217. }
  218. else
  219. {
  220. if (this.writableTextBox != null)
  221. {
  222. this.writableTextBox.Visibility = Visibility.Hidden;
  223. }
  224. }
  225. if (this.writableComboBox.Items.Count == 5)
  226. {
  227. if (this.writableComboBox.SelectedIndex == 1)
  228. { IsCurrentPage = true; }
  229. else
  230. {
  231. IsCurrentPage = false;
  232. }
  233. }
  234. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  235. if (writableComboBox.SelectedItem == null)
  236. {
  237. return;
  238. }
  239. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  240. {
  241. return;
  242. }
  243. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  244. {
  245. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  246. {
  247. case "AllPage":
  248. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  249. {
  250. PageIndexList = pageIndexList;
  251. Text = "1-" + MaxPageRange;
  252. }
  253. break;
  254. case "OddPage":
  255. {
  256. string pageRange = "";
  257. for (int i = 1; i <= MaxPageRange; i++)
  258. {
  259. if (i % 2 != 0 || MaxPageRange == 1)
  260. {
  261. if (string.IsNullOrEmpty(pageRange))
  262. {
  263. pageRange = i.ToString();
  264. }
  265. else
  266. {
  267. pageRange += "," + i;
  268. }
  269. }
  270. }
  271. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  272. {
  273. PageIndexList = pageIndexList;
  274. Text = pageRange;
  275. }
  276. break;
  277. }
  278. case "EvenPage":
  279. {
  280. string pageRange = "";
  281. for (int i = 1; i <= MaxPageRange; i++)
  282. {
  283. if (i % 2 == 0 || MaxPageRange == 1)
  284. {
  285. if (string.IsNullOrEmpty(pageRange))
  286. {
  287. pageRange = i.ToString();
  288. }
  289. else
  290. {
  291. pageRange += "," + i;
  292. }
  293. }
  294. }
  295. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  296. {
  297. PageIndexList = pageIndexList;
  298. Text = pageRange;
  299. }
  300. break;
  301. }
  302. case "CustomPage":
  303. writableTextBox.Focus();
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. SelectionChanged?.Invoke(sender, e);
  310. }
  311. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  312. {
  313. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  314. {
  315. Text = this.writableTextBox.Text;
  316. }
  317. else { Text = ""; }
  318. TextChanged?.Invoke(sender, e);
  319. }
  320. private void writableTextBox_LostFocus(object sender, RoutedEventArgs e)
  321. {
  322. if (CommonHelper.GetPagesInRange(ref pageIndexList, writableTextBox.Text, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  323. {
  324. PageIndexList = pageIndexList;
  325. Text = writableTextBox.Text;
  326. }
  327. else
  328. {
  329. writableTextBox.Text = "";
  330. }
  331. }
  332. private void writableTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  333. {
  334. if (e.Key == Key.Enter)
  335. {
  336. writableComboBox.Focus();
  337. }
  338. }
  339. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  340. {
  341. UpDataPagesInRange();
  342. AllPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_AllPage");
  343. OddPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_OddPage");
  344. EvenPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_EvenPage");
  345. CustomPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_CustomRange");
  346. writableTextBox.Tag = App.MainPageLoader.GetString("WritableComboBox_CustomRangeEge");
  347. }
  348. private void writableTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  349. {
  350. e.Handled = new Regex("[^0-9,-]+").IsMatch(e.Text);
  351. }
  352. }
  353. }