BookmarkContent.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. {
  220. //点击空白
  221. textBlock.Text = text.Text;
  222. listItem.IsSelected = true;
  223. }
  224. }
  225. //if (histotyListBoxItem != null)
  226. //{
  227. // histotyListBoxItem.IsSelected = false;
  228. // TextBlock textBlock1 = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  229. // textBlock1.Visibility = Visibility.Visible;
  230. // textBlock1.Focusable = true;
  231. //}
  232. //TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  233. //TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  234. //if (textBlock.Visibility == Visibility.Collapsed)
  235. //{
  236. // listItem.IsSelected = false;
  237. // //textBlock.Visibility = Visibility.Visible;
  238. //}
  239. //else
  240. //{
  241. // listItem.IsSelected = true;
  242. // //textBlock.Visibility = Visibility.Collapsed;
  243. //}
  244. }
  245. }
  246. private bool isRename = false;
  247. /// <summary>
  248. /// 右键菜单-重命名
  249. /// </summary>
  250. /// <param name="sender"></param>
  251. /// <param name="e"></param>
  252. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  253. {
  254. if (sender is MenuItem)
  255. {
  256. MenuItem menuItem = (MenuItem)sender;
  257. CPDFBookmark bookmark = BookMarkListView.SelectedItem as CPDFBookmark;
  258. if (bookmark != null)
  259. {
  260. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  261. isRename = true;
  262. ListBoxItem listBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  263. histotyListBoxItem = listBoxItem;
  264. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  265. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  266. SetSelectedStatus(listBoxItem, textBox, textBlock);
  267. }
  268. }
  269. }
  270. /// <summary>
  271. /// BookMarkListView,鼠标点击
  272. /// </summary>
  273. /// <param name="sender"></param>
  274. /// <param name="e"></param>
  275. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  276. {
  277. if (e.LeftButton == MouseButtonState.Pressed)
  278. {
  279. var pos = e.GetPosition(BookMarkListView);
  280. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  281. if (result != null)
  282. {
  283. //获取当前鼠标指针下的容器
  284. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  285. if (listBoxItem == null)
  286. {
  287. if (BookMarkListView.SelectedItem != null)
  288. {
  289. ListBoxItem item = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  290. item.IsSelected = false;
  291. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  292. box.Visibility = Visibility.Visible;
  293. BookMarkListView.SelectedItems.Clear();
  294. }
  295. if (histotyListBoxItem != null)
  296. {
  297. var pos1 = e.GetPosition(histotyListBoxItem);
  298. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  299. if (result1 == null)
  300. {
  301. histotyListBoxItem.IsSelected = false;
  302. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  303. textBlock.Visibility = Visibility.Visible;
  304. textBlock.Focusable = true;
  305. }
  306. }
  307. }
  308. else
  309. {
  310. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  311. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  312. if (textBox.IsFocused == false)
  313. {
  314. listBoxItem.Focus();
  315. }
  316. }
  317. }
  318. //BookMarkListView.Focus();
  319. }
  320. }
  321. /// <summary>
  322. /// 右键菜单-删除
  323. /// </summary>
  324. /// <param name="sender"></param>
  325. /// <param name="e"></param>
  326. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  327. {
  328. List<int> pagelist = new List<int>();
  329. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  330. {
  331. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  332. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  333. }
  334. pagelist.Sort();
  335. for (int i = 0; i < pagelist.Count; i++)
  336. {
  337. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  338. if (data != null)
  339. {
  340. viewModel.DeleteCommand.Execute(data);
  341. }
  342. }
  343. }
  344. public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
  345. {
  346. isAdd = true;
  347. viewModel.AddBookmarkCommand.Execute(BookMarkListView);
  348. if (viewModel.IsHasBookmark == false)
  349. {
  350. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  351. ListBoxItem myListBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  352. if (myListBoxItem == null)
  353. {
  354. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  355. BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
  356. }
  357. else
  358. {
  359. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  360. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  361. histotyListBoxItem = myListBoxItem;
  362. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  363. }
  364. }
  365. }
  366. private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  367. {
  368. ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
  369. if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
  370. {
  371. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  372. ListBoxItem myListBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  373. if (myListBoxItem != null)
  374. {
  375. if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
  376. {
  377. BookMarkListView.UpdateLayout();
  378. myListBoxItem.UpdateLayout();
  379. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  380. }
  381. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  382. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  383. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  384. }
  385. }
  386. }
  387. private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
  388. {
  389. if (BookMarkListView.SelectedItems == null)
  390. {
  391. return;
  392. }
  393. if (e.Key == Key.Escape)
  394. {
  395. BookMarkListView.SelectedItems.Clear();
  396. }
  397. //if (e.Key == Key.Delete)
  398. //{
  399. // if (BookMarkListView.SelectedItems.Count > 0)
  400. // {
  401. // //全选删除
  402. // if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
  403. // {
  404. // }
  405. // }
  406. //}
  407. }
  408. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  409. {
  410. //ContextMenu contextMenu = sender as ContextMenu;
  411. if (sender is ContextMenu contextMenu)
  412. {
  413. Trace.WriteLine($"{BookMarkListView.SelectedItems.Count}");
  414. MenuItem rename = contextMenu.Items[0] as MenuItem;
  415. MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
  416. MenuItem del = contextMenu.Items[2] as MenuItem;
  417. rename.IsEnabled = true;
  418. editPageIndex.IsEnabled = true;
  419. if (BookMarkListView.SelectedItems.Count > 1)
  420. {
  421. rename.IsEnabled = false;
  422. editPageIndex.IsEnabled = false;
  423. //isSelects = false;
  424. }
  425. else
  426. {
  427. Trace.WriteLine($"{BookMarkListView.SelectedItems.Count}");
  428. CPDFBookmark bookmark = null;
  429. if (BookMarkListView.SelectedItem != null)
  430. {
  431. bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  432. if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
  433. {
  434. editPageIndex.IsEnabled = false;
  435. return;
  436. }
  437. rename.IsEnabled = true;
  438. editPageIndex.IsEnabled = true;
  439. }
  440. }
  441. }
  442. }
  443. private void MenuChangeItem_Click(object sender, RoutedEventArgs e)
  444. {
  445. if (isSelects == false)
  446. {
  447. if (BookMarkListView.SelectedItem != null)
  448. {
  449. CPDFBookmark bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  450. if (bookmark != null && sender is MenuItem editPageIndex)
  451. {
  452. editPageIndex.CommandParameter = bookmark;
  453. editPageIndex.Command = viewModel.EditPageIndexCommand;
  454. }
  455. }
  456. }
  457. }
  458. }
  459. }