BookmarkContent.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. CleanHistotyListBoxItem(listBoxItem, textBlock);
  94. viewModel.ListViewItemMouseDownCommand.Execute(sender);
  95. }
  96. }
  97. else if (e.RightButton == MouseButtonState.Pressed)
  98. {
  99. CleanHistotyListBoxItem(listBoxItem, textBlock);
  100. }
  101. }
  102. private void CleanHistotyListBoxItem(ListBoxItem listBoxItem, TextBlock textBlock)
  103. {
  104. if (histotyListBoxItem != listBoxItem)
  105. {
  106. if (histotyListBoxItem != null)
  107. {
  108. histotyListBoxItem.IsSelected = false;
  109. textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  110. textBlock.Visibility = Visibility.Visible;
  111. textBlock.Focusable = true;
  112. }
  113. }
  114. }
  115. private bool isSelects = false;
  116. private void BookMarkListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  117. {
  118. MenuItem rename = ContextMenuBook.Items[0] as MenuItem;
  119. MenuItem editPageIndex = ContextMenuBook.Items[1] as MenuItem;
  120. MenuItem del = ContextMenuBook.Items[2] as MenuItem;
  121. var a1 = (sender as ListBox).SelectedItems.Count;
  122. if (a1 >= 1)
  123. {
  124. isSelects = true;
  125. ContextMenuBook.Visibility = Visibility.Visible;
  126. if (a1 > 1)
  127. {
  128. rename.IsEnabled = false;
  129. editPageIndex.IsEnabled = false;
  130. }
  131. if (a1 == 1)
  132. {
  133. isSelects = false;
  134. CPDFBookmark bookmark = null;
  135. if (BookMarkListView.SelectedItem != null)
  136. {
  137. bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  138. if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
  139. {
  140. editPageIndex.IsEnabled = false;
  141. return;
  142. }
  143. rename.IsEnabled = true;
  144. editPageIndex.IsEnabled = true;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. ContextMenuBook.Visibility = Visibility.Collapsed;
  151. isSelects = false;
  152. }
  153. }
  154. /// <summary>
  155. /// ListViewItem双击时选中状态
  156. /// </summary>
  157. /// <param name="listBoxItem"></param>
  158. /// <param name="textBox"></param>
  159. /// <param name="textBlock"></param>
  160. private void SetSelectedStatus(ListBoxItem listBoxItem, TextBox textBox, TextBlock textBlock)
  161. {
  162. if (listBoxItem == null || textBox == null || textBlock == null)
  163. {
  164. return;
  165. }
  166. listBoxItem.IsSelected = true;
  167. listBoxItem.Focus();
  168. textBlock.Visibility = Visibility.Collapsed;
  169. isRename = true;
  170. textBox.Dispatcher.BeginInvoke(new Action(() =>
  171. {
  172. textBox.Focus();
  173. textBox.SelectAll();
  174. //listBoxItem.IsSelected = true;
  175. }));
  176. }
  177. /// <summary>
  178. /// ListViewItem失去焦点
  179. /// </summary>
  180. /// <param name="sender"></param>
  181. /// <param name="e"></param>
  182. private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
  183. {
  184. ListBoxItem listItem = sender as ListBoxItem;
  185. if (listItem != null)
  186. {
  187. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  188. viewModel.LostFocusCommand.Execute(listItem);
  189. //CleanSelectAll(null);
  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. listItem.IsSelected = true;
  200. isRename = false;
  201. }
  202. }
  203. else
  204. {
  205. if (isCleanSelectAll == false)
  206. {
  207. listItem.IsSelected = true;
  208. isAdd = false;
  209. }
  210. }
  211. //if (text.IsSelectionActive == true)
  212. //{
  213. // textBlock.Visibility = Visibility.Visible;
  214. //}
  215. }
  216. else
  217. {
  218. if (isCleanSelectAll == false)
  219. listItem.IsSelected = true;
  220. }
  221. //if (histotyListBoxItem != null)
  222. //{
  223. // histotyListBoxItem.IsSelected = false;
  224. // TextBlock textBlock1 = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  225. // textBlock1.Visibility = Visibility.Visible;
  226. // textBlock1.Focusable = true;
  227. //}
  228. //TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  229. //TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  230. //if (textBlock.Visibility == Visibility.Collapsed)
  231. //{
  232. // listItem.IsSelected = false;
  233. // //textBlock.Visibility = Visibility.Visible;
  234. //}
  235. //else
  236. //{
  237. // listItem.IsSelected = true;
  238. // //textBlock.Visibility = Visibility.Collapsed;
  239. //}
  240. }
  241. }
  242. private bool isRename = false;
  243. /// <summary>
  244. /// 右键菜单-重命名
  245. /// </summary>
  246. /// <param name="sender"></param>
  247. /// <param name="e"></param>
  248. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  249. {
  250. if (sender is MenuItem)
  251. {
  252. MenuItem menuItem = (MenuItem)sender;
  253. CPDFBookmark bookmark = BookMarkListView.SelectedItem as CPDFBookmark;
  254. if (bookmark != null)
  255. {
  256. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  257. isRename = true;
  258. ListBoxItem listBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  259. histotyListBoxItem = listBoxItem;
  260. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  261. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  262. SetSelectedStatus(listBoxItem, textBox, textBlock);
  263. }
  264. }
  265. }
  266. /// <summary>
  267. /// BookMarkListView,鼠标点击
  268. /// </summary>
  269. /// <param name="sender"></param>
  270. /// <param name="e"></param>
  271. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  272. {
  273. if (e.LeftButton == MouseButtonState.Pressed)
  274. {
  275. var pos = e.GetPosition(BookMarkListView);
  276. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  277. if (result != null)
  278. {
  279. //获取当前鼠标指针下的容器
  280. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  281. if (listBoxItem == null)
  282. {
  283. if (BookMarkListView.SelectedItem != null)
  284. {
  285. ListBoxItem item = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  286. item.IsSelected = false;
  287. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  288. box.Visibility = Visibility.Visible;
  289. BookMarkListView.SelectedItems.Clear();
  290. }
  291. if (histotyListBoxItem != null)
  292. {
  293. var pos1 = e.GetPosition(histotyListBoxItem);
  294. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  295. if (result1 == null)
  296. {
  297. histotyListBoxItem.IsSelected = false;
  298. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  299. textBlock.Visibility = Visibility.Visible;
  300. textBlock.Focusable = true;
  301. }
  302. }
  303. }
  304. else
  305. {
  306. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  307. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  308. if (textBox.IsFocused == false)
  309. {
  310. listBoxItem.Focus();
  311. }
  312. }
  313. }
  314. //BookMarkListView.Focus();
  315. }
  316. }
  317. /// <summary>
  318. /// 右键菜单-删除
  319. /// </summary>
  320. /// <param name="sender"></param>
  321. /// <param name="e"></param>
  322. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  323. {
  324. List<int> pagelist = new List<int>();
  325. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  326. {
  327. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  328. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  329. }
  330. pagelist.Sort();
  331. for (int i = 0; i < pagelist.Count; i++)
  332. {
  333. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  334. if (data != null)
  335. {
  336. viewModel.DeleteCommand.Execute(data);
  337. }
  338. }
  339. }
  340. public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
  341. {
  342. isAdd = true;
  343. viewModel.AddBookmarkCommand.Execute(BookMarkListView);
  344. if (viewModel.IsHasBookmark == false)
  345. {
  346. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  347. ListBoxItem myListBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  348. if (myListBoxItem == null)
  349. {
  350. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  351. BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
  352. }
  353. else
  354. {
  355. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  356. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  357. histotyListBoxItem = myListBoxItem;
  358. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  359. }
  360. }
  361. }
  362. private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  363. {
  364. ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
  365. if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
  366. {
  367. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  368. ListBoxItem myListBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  369. if (myListBoxItem != null)
  370. {
  371. if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
  372. {
  373. BookMarkListView.UpdateLayout();
  374. myListBoxItem.UpdateLayout();
  375. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  376. }
  377. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  378. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  379. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  380. }
  381. }
  382. }
  383. private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
  384. {
  385. if (BookMarkListView.SelectedItems == null)
  386. {
  387. return;
  388. }
  389. if (e.Key == Key.Escape)
  390. {
  391. BookMarkListView.SelectedItems.Clear();
  392. }
  393. //if (e.Key == Key.Delete)
  394. //{
  395. // if (BookMarkListView.SelectedItems.Count > 0)
  396. // {
  397. // //全选删除
  398. // if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
  399. // {
  400. // }
  401. // }
  402. //}
  403. }
  404. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  405. {
  406. //ContextMenu contextMenu = sender as ContextMenu;
  407. if (sender is ContextMenu contextMenu)
  408. {
  409. Trace.WriteLine($"{BookMarkListView.SelectedItems.Count}");
  410. MenuItem rename = contextMenu.Items[0] as MenuItem;
  411. MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
  412. MenuItem del = contextMenu.Items[2] as MenuItem;
  413. rename.IsEnabled = true;
  414. editPageIndex.IsEnabled = true;
  415. if (BookMarkListView.SelectedItems.Count > 1)
  416. {
  417. rename.IsEnabled = false;
  418. editPageIndex.IsEnabled = false;
  419. //isSelects = false;
  420. }
  421. else
  422. {
  423. Trace.WriteLine($"{BookMarkListView.SelectedItems.Count}");
  424. CPDFBookmark bookmark = null;
  425. if (BookMarkListView.SelectedItem != null)
  426. {
  427. bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  428. if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
  429. {
  430. editPageIndex.IsEnabled = false;
  431. return;
  432. }
  433. rename.IsEnabled = true;
  434. editPageIndex.IsEnabled = true;
  435. }
  436. }
  437. }
  438. }
  439. private void MenuChangeItem_Click(object sender, RoutedEventArgs e)
  440. {
  441. if (isSelects == false)
  442. {
  443. if (BookMarkListView.SelectedItem != null)
  444. {
  445. CPDFBookmark bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  446. if (bookmark != null && sender is MenuItem editPageIndex)
  447. {
  448. editPageIndex.CommandParameter = bookmark;
  449. editPageIndex.Command = viewModel.EditPageIndexCommand;
  450. }
  451. }
  452. }
  453. }
  454. }
  455. }