WritableComboBox.xaml.cs 15 KB

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