WritableComboBox.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. ErrorBorder.Visibility = Visibility.Collapsed;
  225. this.writableTextBox.Visibility = Visibility.Hidden;
  226. }
  227. }
  228. if (this.writableComboBox.Items.Count == 5)
  229. {
  230. if (this.writableComboBox.SelectedIndex == 1)
  231. { IsCurrentPage = true; }
  232. else
  233. {
  234. IsCurrentPage = false;
  235. }
  236. }
  237. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  238. if (writableComboBox.SelectedItem == null)
  239. {
  240. return;
  241. }
  242. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  243. {
  244. return;
  245. }
  246. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  247. {
  248. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  249. {
  250. case "AllPage":
  251. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  252. {
  253. PageIndexList = pageIndexList;
  254. Text = "1-" + MaxPageRange;
  255. }
  256. break;
  257. case "OddPage":
  258. {
  259. string pageRange = "";
  260. for (int i = 1; i <= MaxPageRange; i++)
  261. {
  262. if (i % 2 != 0 || MaxPageRange == 1)
  263. {
  264. if (string.IsNullOrEmpty(pageRange))
  265. {
  266. pageRange = i.ToString();
  267. }
  268. else
  269. {
  270. pageRange += "," + i;
  271. }
  272. }
  273. }
  274. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  275. {
  276. PageIndexList = pageIndexList;
  277. Text = pageRange;
  278. }
  279. break;
  280. }
  281. case "EvenPage":
  282. {
  283. string pageRange = "";
  284. for (int i = 1; i <= MaxPageRange; i++)
  285. {
  286. if (i % 2 == 0 || MaxPageRange == 1)
  287. {
  288. if (string.IsNullOrEmpty(pageRange))
  289. {
  290. pageRange = i.ToString();
  291. }
  292. else
  293. {
  294. pageRange += "," + i;
  295. }
  296. }
  297. }
  298. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  299. {
  300. PageIndexList = pageIndexList;
  301. Text = pageRange;
  302. }
  303. break;
  304. }
  305. case "CustomPage":
  306. writableTextBox.Text = string.Empty;
  307. writableTextBox.Focus();
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. SelectionChanged?.Invoke(sender, e);
  314. }
  315. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  316. {
  317. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  318. {
  319. Text = this.writableTextBox.Text;
  320. }
  321. else { Text = ""; }
  322. TextChanged?.Invoke(sender, e);
  323. }
  324. private void writableTextBox_LostFocus(object sender, RoutedEventArgs e)
  325. {
  326. ErrorBorder.Visibility = Visibility.Collapsed;
  327. if (!IsloseFocus) { return; }
  328. if (CommonHelper.GetPagesInRange(ref pageIndexList, writableTextBox.Text, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  329. {
  330. PageIndexList = pageIndexList;
  331. Text = writableTextBox.Text;
  332. }
  333. else
  334. {
  335. AlertsMessage alertsMessage = new AlertsMessage();
  336. //alertsMessage.ShowDialog($"Value must be between 1 and {MaxPageRange}", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"), IconType.Tip);
  337. alertsMessage.ShowDialog($"{App.MainPageLoader.GetString("PageEdit_SplitErrorTile")}{MaxPageRange}", $"{App.MainPageLoader.GetString("PageEdit_SplitErrorContent")}", App.ServiceLoader.GetString("Text_ok"), IconType.Tip);
  338. ErrorBorder.Visibility = Visibility.Visible;
  339. writableTextBox.Text = "";
  340. }
  341. }
  342. private void writableTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  343. {
  344. if (e.Key == Key.Enter)
  345. {
  346. writableComboBox.Focus();
  347. }
  348. }
  349. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  350. {
  351. //230621 重复调用 ,OnMaxPageRangeChanged 有调用此方法
  352. //注释 是为了 选中页面拆分,需要选自定义,并且显示选中的页码
  353. //UpDataPagesInRange();
  354. AllPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_AllPage");
  355. OddPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_OddPage");
  356. EvenPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_EvenPage");
  357. CustomPageItem.Content = App.MainPageLoader.GetString("WritableComboBox_CustomRange");
  358. writableTextBox.Tag = App.MainPageLoader.GetString("WritableComboBox_CustomRangeEge");
  359. }
  360. private void writableTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  361. {
  362. e.Handled = new Regex("[^0-9,-]+").IsMatch(e.Text);
  363. }
  364. private void writableComboBox_MouseLeave(object sender, MouseEventArgs e)
  365. {
  366. IsloseFocus = true;
  367. }
  368. private void writableComboBox_MouseEnter(object sender, MouseEventArgs e)
  369. {
  370. IsloseFocus = false;
  371. }
  372. private void writableTextBox_GotFocus(object sender, RoutedEventArgs e)
  373. {
  374. ErrorBorder.Visibility = Visibility.Collapsed;
  375. }
  376. }
  377. }