BookmarkContent.xaml.cs 19 KB

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