BookmarkContent.xaml.cs 17 KB

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