WritableComboBox.xaml.cs 17 KB

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