WritableComboBox.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text.RegularExpressions;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using ComPDFKit.Controls.Helper;
  9. namespace ComPDFKit.Controls.Common.BaseControl
  10. {
  11. public partial class WritableComboBox : UserControl
  12. {
  13. //判断鼠标是否悬停在此按钮上,通过MouseLeave-Enter赋值。
  14. public bool IsloseFocus = true;
  15. public bool IsCurrentPage
  16. {
  17. get { return (bool)GetValue(IsCurrentPageProperty); }
  18. set { SetValue(IsCurrentPageProperty, value); }
  19. }
  20. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  21. public static readonly DependencyProperty IsCurrentPageProperty =
  22. DependencyProperty.Register("IsCurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  23. public Visibility IsAllPageVisible
  24. {
  25. get { return (Visibility)GetValue(IsAllPageVisibleProperty); }
  26. set
  27. {
  28. SetValue(IsAllPageVisibleProperty, value);
  29. }
  30. }
  31. // Using a DependencyProperty as the backing store for IsAllPageVisible. This enables animation, styling, binding, etc...
  32. public static readonly DependencyProperty IsAllPageVisibleProperty =
  33. DependencyProperty.Register("IsAllPageVisible", typeof(Visibility), typeof(WritableComboBox), new PropertyMetadata(Visibility.Visible, (d, e) =>
  34. {
  35. if ((Visibility)e.NewValue != Visibility.Visible)
  36. {
  37. (d as WritableComboBox).SetIndexByVisiblity((Visibility)e.NewValue);
  38. }
  39. }));
  40. private void SetIndexByVisiblity(Visibility visible)
  41. {
  42. writableComboBox.SelectedIndex = 1;
  43. }
  44. public bool CurrentPage
  45. {
  46. get { return (bool)GetValue(CurrentPageProperty); }
  47. set { SetValue(CurrentPageProperty, value); }
  48. }
  49. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  50. public static readonly DependencyProperty CurrentPageProperty =
  51. DependencyProperty.Register("CurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
  52. public bool EvenPageIsEnabled
  53. {
  54. get { return (bool)GetValue(EvenPageIsEnabledProperty); }
  55. set { SetValue(EvenPageIsEnabledProperty, value); }
  56. }
  57. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  58. public static readonly DependencyProperty EvenPageIsEnabledProperty =
  59. DependencyProperty.Register("EvenPageIsEnabled", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(true));
  60. public string SelectedIndex
  61. {
  62. get { return (string)GetValue(SelectedIndexProperty); }
  63. set { SetValue(SelectedIndexProperty, value); }
  64. }
  65. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  66. public static readonly DependencyProperty SelectedIndexProperty =
  67. DependencyProperty.Register("SelectedIndex", typeof(string), typeof(WritableComboBox), new PropertyMetadata("0"));
  68. public string Text
  69. {
  70. get { return (string)GetValue(TextProperty); }
  71. set { SetValue(TextProperty, value); }
  72. }
  73. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  74. public static readonly DependencyProperty TextProperty =
  75. DependencyProperty.Register("Text", typeof(string), typeof(WritableComboBox), new PropertyMetadata(""));
  76. private List<int> pageIndexList = new List<int>();
  77. public List<int> PageIndexList
  78. {
  79. get { return (List<int>)GetValue(PageIndexListProperty); }
  80. set { SetValue(PageIndexListProperty, value); }
  81. }
  82. // Using a DependencyProperty as the backing store for PageIndexList. This enables animation, styling, binding, etc...
  83. public static readonly DependencyProperty PageIndexListProperty =
  84. DependencyProperty.Register("PageIndexList", typeof(List<int>), typeof(WritableComboBox), new PropertyMetadata(new List<int>()));
  85. public int MaxPageRange
  86. {
  87. get { return (int)GetValue(MaxPageRangeProperty); }
  88. set { SetValue(MaxPageRangeProperty, value); }
  89. }
  90. // Using a DependencyProperty as the backing store for MaxPageRange. This enables animation, styling, binding, etc...
  91. public static readonly DependencyProperty MaxPageRangeProperty =
  92. DependencyProperty.Register("MaxPageRange", typeof(int), typeof(WritableComboBox), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(OnMaxPageRangeChanged)));
  93. private static void OnMaxPageRangeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  94. {
  95. int value = Convert.ToInt32(e.NewValue);
  96. if (value > 0)
  97. {
  98. (d as WritableComboBox).UpDataPagesInRange();
  99. }
  100. }
  101. private void UpDataPagesInRange()
  102. {
  103. if (writableComboBox.SelectedItem == null)
  104. {
  105. return;
  106. }
  107. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  108. {
  109. return;
  110. }
  111. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  112. {
  113. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  114. {
  115. case "AllPage":
  116. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  117. {
  118. PageIndexList = pageIndexList;
  119. Text = "1-" + MaxPageRange;
  120. }
  121. break;
  122. case "OddPage":
  123. {
  124. string pageRange = "";
  125. for (int i = 1; i <= MaxPageRange; i++)
  126. {
  127. if (i % 2 != 0 || MaxPageRange == 1)
  128. {
  129. if (string.IsNullOrEmpty(pageRange))
  130. {
  131. pageRange = i.ToString();
  132. }
  133. else
  134. {
  135. pageRange += "," + i;
  136. }
  137. }
  138. }
  139. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  140. {
  141. PageIndexList = pageIndexList;
  142. Text = pageRange;
  143. }
  144. break;
  145. }
  146. case "EvenPage":
  147. {
  148. string pageRange = "";
  149. for (int i = 1; i <= MaxPageRange; i++)
  150. {
  151. if (i % 2 == 0 || MaxPageRange == 1)
  152. {
  153. if (string.IsNullOrEmpty(pageRange))
  154. {
  155. pageRange = i.ToString();
  156. }
  157. else
  158. {
  159. pageRange += "," + i;
  160. }
  161. }
  162. }
  163. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  164. {
  165. PageIndexList = pageIndexList;
  166. Text = pageRange;
  167. }
  168. break;
  169. }
  170. case "CustomPage":
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. }
  177. public ItemCollection Items
  178. {
  179. get { return (ItemCollection)GetValue(ItemsProperty); }
  180. set { SetValue(ItemsProperty, value); }
  181. }
  182. public static readonly DependencyProperty ItemsProperty =
  183. DependencyProperty.Register("Items", typeof(ItemCollection), typeof(WritableComboBox), new PropertyMetadata());
  184. /// <summary>
  185. /// 把子控件的事件传递出去,方便绑定
  186. /// </summary>
  187. public event RoutedEventHandler SelectionChanged;
  188. /// <summary>
  189. /// 把子控件的事件传递出去,方便绑定
  190. /// </summary>
  191. public event RoutedEventHandler TextChanged;
  192. public WritableComboBox()
  193. {
  194. InitializeComponent();
  195. this.Items = this.writableComboBox.Items;
  196. }
  197. private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  198. {
  199. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  200. {
  201. if (this.writableComboBox.ActualWidth == 0) { this.writableTextBox.Width = 210; this.writableTextBox.Visibility = Visibility.Visible; return; }
  202. this.writableTextBox.Width = this.writableComboBox.ActualWidth - 28;
  203. Trace.WriteLine(this.writableComboBox.ActualWidth);
  204. this.writableTextBox.Visibility = Visibility.Visible;
  205. }
  206. else
  207. {
  208. if (this.writableTextBox != null)
  209. {
  210. ErrorBorder.Visibility = Visibility.Collapsed;
  211. this.writableTextBox.Visibility = Visibility.Hidden;
  212. }
  213. }
  214. if (this.writableComboBox.Items.Count == 5)
  215. {
  216. if (this.writableComboBox.SelectedIndex == 1)
  217. { IsCurrentPage = true; }
  218. else
  219. {
  220. IsCurrentPage = false;
  221. }
  222. }
  223. this.SelectedIndex = this.writableComboBox.SelectedIndex.ToString();
  224. if (writableComboBox.SelectedItem == null)
  225. {
  226. return;
  227. }
  228. if (writableComboBox.SelectedItem as ComboBoxItem == null)
  229. {
  230. return;
  231. }
  232. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  233. {
  234. PopTipPageRange.IsOpen = false;
  235. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  236. {
  237. case "AllPage":
  238. if (CommonHelper.GetPagesInRange(ref pageIndexList, "1-" + MaxPageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  239. {
  240. PageIndexList = pageIndexList;
  241. Text = "1-" + MaxPageRange;
  242. }
  243. break;
  244. case "OddPage":
  245. {
  246. string pageRange = "";
  247. for (int i = 1; i <= MaxPageRange; i++)
  248. {
  249. if (i % 2 != 0 || MaxPageRange == 1)
  250. {
  251. if (string.IsNullOrEmpty(pageRange))
  252. {
  253. pageRange = i.ToString();
  254. }
  255. else
  256. {
  257. pageRange += "," + i;
  258. }
  259. }
  260. }
  261. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  262. {
  263. PageIndexList = pageIndexList;
  264. Text = pageRange;
  265. }
  266. break;
  267. }
  268. case "EvenPage":
  269. {
  270. string pageRange = "";
  271. for (int i = 1; i <= MaxPageRange; i++)
  272. {
  273. if (i % 2 == 0 || MaxPageRange == 1)
  274. {
  275. if (string.IsNullOrEmpty(pageRange))
  276. {
  277. pageRange = i.ToString();
  278. }
  279. else
  280. {
  281. pageRange += "," + i;
  282. }
  283. }
  284. }
  285. if (CommonHelper.GetPagesInRange(ref pageIndexList, pageRange, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  286. {
  287. PageIndexList = pageIndexList;
  288. Text = pageRange;
  289. }
  290. break;
  291. }
  292. case "CustomPage":
  293. writableTextBox.Text = string.Empty;
  294. writableTextBox.Focus();
  295. break;
  296. default:
  297. break;
  298. }
  299. }
  300. SelectionChanged?.Invoke(sender, e);
  301. }
  302. private void writableTextBox_TextChange(object sender, TextChangedEventArgs e)
  303. {
  304. if (this.writableComboBox.SelectedIndex == this.writableComboBox.Items.Count - 1)
  305. {
  306. Text = this.writableTextBox.Text;
  307. }
  308. else { Text = ""; }
  309. TextChanged?.Invoke(sender, e);
  310. }
  311. private void writableTextBox_LostFocus(object sender, RoutedEventArgs e)
  312. {
  313. ErrorBorder.Visibility = Visibility.Collapsed;
  314. if (!IsloseFocus) { return; }
  315. if (CommonHelper.GetPagesInRange(ref pageIndexList, writableTextBox.Text, MaxPageRange, new char[] { ',' }, new char[] { '-' }))
  316. {
  317. PageIndexList = pageIndexList;
  318. Text = writableTextBox.Text;
  319. // PopTipPageRange.IsOpen = false;
  320. }
  321. else
  322. {
  323. //PopTipPageRange.IsOpen = true;
  324. //TxtError.Text = string.Format($"{LanguageHelper.CommonManager.GetString("PageEdit_SplitErrorTile")}{MaxPageRange}", $"{LanguageHelper.CommonManager.GetString("PageEdit_SplitErrorContent")}");
  325. // TxtError.Text = LanguageHelper.CommonManager.GetString("Main_PageRangedWarning");
  326. //MessageBox.Show($"{LanguageHelper.CommonManager.GetString("PageEdit_SplitErrorTile")}{MaxPageRange}", LanguageHelper.CommonManager.GetString("PageEdit_SplitErrorContent"), MessageBoxButton.OK, MessageBoxImage.Information);
  327. ErrorBorder.Visibility = Visibility.Visible;
  328. writableTextBox.Text = "";
  329. }
  330. }
  331. private void writableTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  332. {
  333. if (e.Key == Key.Enter)
  334. {
  335. writableComboBox.Focus();
  336. }
  337. }
  338. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  339. {
  340. //230621 重复调用 ,OnMaxPageRangeChanged 有调用此方法
  341. //注释 是为了 选中页面拆分,需要选自定义,并且显示选中的页码
  342. //UpDataPagesInRange();
  343. AllPageItem.Content = LanguageHelper.CommonManager.GetString("Option_AllPage");
  344. OddPageItem.Content = LanguageHelper.CommonManager.GetString("Option_OddPages");
  345. EvenPageItem.Content = LanguageHelper.CommonManager.GetString("Option_EvenPages");
  346. CustomPageItem.Content = LanguageHelper.CommonManager.GetString("Option_CustomPages");
  347. writableTextBox.Tag = LanguageHelper.DocEditorManager.GetString("Holder_Custom");
  348. writableTextBox.Width = this.Width - 25;
  349. }
  350. private void writableTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  351. {
  352. e.Handled = new Regex("[^0-9,-]+").IsMatch(e.Text);
  353. }
  354. private void writableComboBox_MouseLeave(object sender, MouseEventArgs e)
  355. {
  356. IsloseFocus = true;
  357. }
  358. private void writableComboBox_MouseEnter(object sender, MouseEventArgs e)
  359. {
  360. IsloseFocus = false;
  361. }
  362. private void writableTextBox_GotFocus(object sender, RoutedEventArgs e)
  363. {
  364. ErrorBorder.Visibility = Visibility.Collapsed;
  365. }
  366. private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
  367. {
  368. if (sender is WritableComboBox comboBox)
  369. {
  370. if (comboBox.IsEnabled)
  371. {
  372. //还原透明度状态
  373. this.Opacity = 1;
  374. writableTextBox.Dispatcher.BeginInvoke(new Action(() =>
  375. {
  376. writableTextBox.Text = Text;
  377. writableTextBox.Focus();
  378. TextBlock textBlock = CommonHelper.PageEditHelper.FindVisualChild<TextBlock>(writableTextBox);
  379. if (textBlock != null)
  380. {
  381. // textBlock.Text = LanguageHelper.CommonManager.GetString("Main_PageRange_EG");
  382. }
  383. }));
  384. }
  385. else
  386. {
  387. //置灰显示
  388. this.Opacity = 0.6;
  389. ErrorBorder.Visibility = Visibility.Collapsed;
  390. if (writableComboBox.SelectedItem == null)
  391. {
  392. return;
  393. }
  394. if ((writableComboBox.SelectedItem as ComboBoxItem).Tag != null)
  395. {
  396. switch ((writableComboBox.SelectedItem as ComboBoxItem).Tag.ToString())
  397. {
  398. case "CustomPage":
  399. writableTextBox.Dispatcher.BeginInvoke(new Action(() =>
  400. {
  401. writableTextBox.Text = string.Empty;
  402. TextBlock textBlock = CommonHelper.PageEditHelper.FindVisualChild<TextBlock>(writableTextBox);
  403. if (textBlock != null)
  404. {
  405. textBlock.Text = null;
  406. }
  407. }));
  408. break;
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. }