BookmarkContent.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using ComPDFKit.PDFDocument;
  2. using Microsoft.Office.Interop.Word;
  3. using PDF_Master.EventAggregators;
  4. using PDF_Master.Helper;
  5. using PDF_Master.ViewModels.BOTA;
  6. using Prism.Events;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Animation;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
  25. namespace PDF_Master.Views.BOTA
  26. {
  27. /// <summary>
  28. /// BookmarkContent.xaml 的交互逻辑
  29. /// </summary>
  30. public partial class BookmarkContent : UserControl
  31. {
  32. private BookmarkContentViewModel viewModel = null;
  33. /// <summary>
  34. /// 上一个ListBoxItem,为选中状态做准备
  35. /// </summary>
  36. public ListBoxItem histotyListBoxItem = null;
  37. private bool isAdd = false;
  38. private string unicode = "";
  39. private bool isCleanSelectAll = false;
  40. public BookmarkContent(IEventAggregator eventAggregator)
  41. {
  42. InitializeComponent();
  43. viewModel = this.DataContext as BookmarkContentViewModel;
  44. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  45. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  46. }
  47. private void CleanSelectAll(CleanSelectAllArgs obj)
  48. {
  49. isCleanSelectAll = true;
  50. BookMarkListView.SelectedIndex = -1;
  51. foreach (var item in BookMarkListView.Items)
  52. {
  53. ListBoxItem listItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(item));
  54. if (listItem != null)
  55. {
  56. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  57. //TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  58. if (textBlock.Visibility == Visibility.Collapsed)
  59. {
  60. textBlock.Visibility = Visibility.Visible;
  61. }
  62. }
  63. }
  64. //BookMarkListView.SelectedItems.Clear();
  65. }
  66. /// <summary>
  67. ///ListViewItem,鼠标左键点击
  68. /// </summary>
  69. /// <param name="sender"></param>
  70. /// <param name="e"></param>
  71. private void ListViewItem_MouseButtonDown(object sender, MouseButtonEventArgs e)
  72. {
  73. object[] objects = new object[] { sender, e };
  74. TextBlock textBlock = null;
  75. ListBoxItem listBoxItem = (sender as ListBoxItem);
  76. if (e.LeftButton == MouseButtonState.Pressed)
  77. {
  78. if (e.ClickCount >= 2)
  79. {
  80. histotyListBoxItem = listBoxItem;
  81. if (e.OriginalSource is TextBlock)
  82. {
  83. textBlock = (TextBlock)e.OriginalSource;
  84. if (textBlock != null)
  85. {
  86. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  87. SetSelectedStatus(listBoxItem, textBox, textBlock);
  88. }
  89. }
  90. }
  91. else if (e.ClickCount == 1)
  92. {
  93. isRename = false;
  94. CleanHistotyListBoxItem(listBoxItem, textBlock);
  95. viewModel.ListViewItemMouseDownCommand.Execute(sender);
  96. }
  97. }
  98. else if (e.RightButton == MouseButtonState.Pressed)
  99. {
  100. CleanHistotyListBoxItem(listBoxItem, textBlock);
  101. }
  102. }
  103. private void CleanHistotyListBoxItem(ListBoxItem listBoxItem, TextBlock textBlock)
  104. {
  105. if (histotyListBoxItem != listBoxItem)
  106. {
  107. if (histotyListBoxItem != null)
  108. {
  109. histotyListBoxItem.IsSelected = false;
  110. textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  111. textBlock.Visibility = Visibility.Visible;
  112. textBlock.Focusable = true;
  113. }
  114. }
  115. }
  116. private bool isSelects = false;
  117. private void BookMarkListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  118. {
  119. MenuItem rename = ContextMenuBook.Items[0] as MenuItem;
  120. MenuItem editPageIndex = ContextMenuBook.Items[1] as MenuItem;
  121. MenuItem del = ContextMenuBook.Items[2] as MenuItem;
  122. var a1 = (sender as ListBox).SelectedItems.Count;
  123. if (a1 >= 1)
  124. {
  125. isSelects = true;
  126. ContextMenuBook.Visibility = Visibility.Visible;
  127. if (a1 > 1)
  128. {
  129. rename.IsEnabled = false;
  130. editPageIndex.IsEnabled = false;
  131. }
  132. if (a1 == 1)
  133. {
  134. isSelects = false;
  135. CPDFBookmark bookmark = null;
  136. if (BookMarkListView.SelectedItem != null)
  137. {
  138. bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  139. if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
  140. {
  141. editPageIndex.IsEnabled = false;
  142. return;
  143. }
  144. rename.IsEnabled = true;
  145. editPageIndex.IsEnabled = true;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. ContextMenuBook.Visibility = Visibility.Collapsed;
  152. isSelects = false;
  153. }
  154. }
  155. /// <summary>
  156. /// ListViewItem双击时选中状态
  157. /// </summary>
  158. /// <param name="listBoxItem"></param>
  159. /// <param name="textBox"></param>
  160. /// <param name="textBlock"></param>
  161. private void SetSelectedStatus(ListBoxItem listBoxItem, TextBox textBox, TextBlock textBlock)
  162. {
  163. if (listBoxItem == null || textBox == null || textBlock == null)
  164. {
  165. return;
  166. }
  167. listBoxItem.IsSelected = true;
  168. listBoxItem.Focus();
  169. textBlock.Visibility = Visibility.Collapsed;
  170. isRename = true;
  171. textBox.Dispatcher.BeginInvoke(new Action(() =>
  172. {
  173. textBox.Focus();
  174. textBox.SelectAll();
  175. //listBoxItem.IsSelected = true;
  176. }));
  177. }
  178. /// <summary>
  179. /// ListViewItem失去焦点
  180. /// </summary>
  181. /// <param name="sender"></param>
  182. /// <param name="e"></param>
  183. private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
  184. {
  185. ListBoxItem listItem = sender as ListBoxItem;
  186. if (listItem != null)
  187. {
  188. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  189. viewModel.LostFocusCommand.Execute(listItem);
  190. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  191. TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  192. if (textBlock.Visibility != Visibility.Visible)
  193. {
  194. if (isAdd == false || isRename == false)
  195. {
  196. listItem.IsSelected = false;
  197. if (isRename)
  198. {
  199. isRename = false;
  200. listItem.IsSelected = true;
  201. }
  202. if (listItem.IsSelected == false)
  203. {
  204. textBlock.Visibility = Visibility.Visible;
  205. }
  206. }
  207. else
  208. {
  209. if (isCleanSelectAll == false)
  210. {
  211. listItem.IsSelected = true;
  212. isAdd = false;
  213. }
  214. }
  215. }
  216. else
  217. {
  218. if (isCleanSelectAll == false)
  219. {
  220. //点击空白
  221. //textBlock.Text = text.Text;
  222. listItem.IsSelected = true;
  223. }
  224. }
  225. }
  226. }
  227. private bool isRename = false;
  228. /// <summary>
  229. /// 右键菜单-重命名
  230. /// </summary>
  231. /// <param name="sender"></param>
  232. /// <param name="e"></param>
  233. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  234. {
  235. if (sender is MenuItem)
  236. {
  237. MenuItem menuItem = (MenuItem)sender;
  238. CPDFBookmark bookmark = BookMarkListView.SelectedItem as CPDFBookmark;
  239. if (bookmark != null)
  240. {
  241. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  242. isRename = true;
  243. ListBoxItem listBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  244. histotyListBoxItem = listBoxItem;
  245. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  246. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  247. SetSelectedStatus(listBoxItem, textBox, textBlock);
  248. }
  249. }
  250. }
  251. /// <summary>
  252. /// BookMarkListView,鼠标点击
  253. /// </summary>
  254. /// <param name="sender"></param>
  255. /// <param name="e"></param>
  256. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  257. {
  258. if (e.LeftButton == MouseButtonState.Pressed)
  259. {
  260. var pos = e.GetPosition(BookMarkListView);
  261. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  262. if (result != null)
  263. {
  264. //获取当前鼠标指针下的容器
  265. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  266. if (listBoxItem == null)
  267. {
  268. if (BookMarkListView.SelectedItem != null)
  269. {
  270. ListBoxItem item = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  271. item.IsSelected = false;
  272. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  273. box.Visibility = Visibility.Visible;
  274. BookMarkListView.SelectedItems.Clear();
  275. }
  276. if (histotyListBoxItem != null)
  277. {
  278. var pos1 = e.GetPosition(histotyListBoxItem);
  279. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  280. if (result1 == null)
  281. {
  282. histotyListBoxItem.IsSelected = false;
  283. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  284. textBlock.Visibility = Visibility.Visible;
  285. textBlock.Focusable = true;
  286. }
  287. }
  288. }
  289. else
  290. {
  291. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  292. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  293. if (textBox.IsFocused == false)
  294. {
  295. listBoxItem.Focus();
  296. }
  297. }
  298. }
  299. //BookMarkListView.Focus();
  300. }
  301. }
  302. /// <summary>
  303. /// 右键菜单-删除
  304. /// </summary>
  305. /// <param name="sender"></param>
  306. /// <param name="e"></param>
  307. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  308. {
  309. List<int> pagelist = new List<int>();
  310. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  311. {
  312. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  313. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  314. }
  315. pagelist.Sort();
  316. for (int i = 0; i < pagelist.Count; i++)
  317. {
  318. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  319. if (data != null)
  320. {
  321. viewModel.DeleteCommand.Execute(data);
  322. }
  323. }
  324. }
  325. public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
  326. {
  327. isAdd = true;
  328. isRename = false;
  329. viewModel.AddBookmarkCommand.Execute(BookMarkListView);
  330. if (viewModel.IsHasBookmark == false)
  331. {
  332. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  333. ListBoxItem myListBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  334. if (myListBoxItem == null)
  335. {
  336. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  337. BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
  338. }
  339. else
  340. {
  341. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  342. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  343. histotyListBoxItem = myListBoxItem;
  344. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  345. }
  346. }
  347. }
  348. private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  349. {
  350. ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
  351. if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
  352. {
  353. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  354. ListBoxItem myListBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  355. if (myListBoxItem != null)
  356. {
  357. if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
  358. {
  359. BookMarkListView.UpdateLayout();
  360. myListBoxItem.UpdateLayout();
  361. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  362. }
  363. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  364. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  365. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  366. }
  367. }
  368. }
  369. private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
  370. {
  371. if (BookMarkListView.SelectedItems == null)
  372. {
  373. return;
  374. }
  375. if (e.Key == Key.Escape)
  376. {
  377. BookMarkListView.SelectedItems.Clear();
  378. }
  379. //if (e.Key == Key.Delete)
  380. //{
  381. // if (BookMarkListView.SelectedItems.Count > 0)
  382. // {
  383. // //全选删除
  384. // if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
  385. // {
  386. // }
  387. // }
  388. //}
  389. }
  390. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  391. {
  392. //ContextMenu contextMenu = sender as ContextMenu;
  393. if (sender is ContextMenu contextMenu)
  394. {
  395. Trace.WriteLine($"{BookMarkListView.SelectedItems.Count}");
  396. MenuItem rename = contextMenu.Items[0] as MenuItem;
  397. MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
  398. MenuItem del = contextMenu.Items[2] as MenuItem;
  399. rename.IsEnabled = true;
  400. editPageIndex.IsEnabled = true;
  401. if (BookMarkListView.SelectedItems.Count > 1)
  402. {
  403. rename.IsEnabled = false;
  404. editPageIndex.IsEnabled = false;
  405. //isSelects = false;
  406. }
  407. else
  408. {
  409. Trace.WriteLine($"{BookMarkListView.SelectedItems.Count}");
  410. CPDFBookmark bookmark = null;
  411. if (BookMarkListView.SelectedItem != null)
  412. {
  413. bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  414. if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
  415. {
  416. editPageIndex.IsEnabled = false;
  417. return;
  418. }
  419. rename.IsEnabled = true;
  420. editPageIndex.IsEnabled = true;
  421. }
  422. }
  423. }
  424. }
  425. private void MenuChangeItem_Click(object sender, RoutedEventArgs e)
  426. {
  427. if (isSelects == false)
  428. {
  429. if (BookMarkListView.SelectedItem != null)
  430. {
  431. CPDFBookmark bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  432. if (bookmark != null && sender is MenuItem editPageIndex)
  433. {
  434. editPageIndex.CommandParameter = bookmark;
  435. editPageIndex.Command = viewModel.EditPageIndexCommand;
  436. }
  437. }
  438. }
  439. }
  440. }
  441. }