BookmarkContent.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. private ListBoxItem histotyListBoxItem = null;
  35. private bool isAdd = false;
  36. private string unicode = "";
  37. public BookmarkContent()
  38. {
  39. InitializeComponent();
  40. viewModel = this.DataContext as BookmarkContentViewModel;
  41. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  42. }
  43. public BookmarkContent(IEventAggregator eventAggregator) : this()
  44. {
  45. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  46. }
  47. private void CleanSelectAll(CleanSelectAllArgs obj)
  48. {
  49. foreach (var item in BookMarkListView.SelectedItems)
  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. listBoxItem.IsSelected = true;
  152. listBoxItem.Focus();
  153. textBlock.Visibility = Visibility.Collapsed;
  154. isRename = true;
  155. textBox.Dispatcher.BeginInvoke(new Action(() =>
  156. {
  157. textBox.Focus();
  158. textBox.SelectAll();
  159. //listBoxItem.IsSelected = true;
  160. }));
  161. }
  162. /// <summary>
  163. /// ListViewItem失去焦点
  164. /// </summary>
  165. /// <param name="sender"></param>
  166. /// <param name="e"></param>
  167. private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
  168. {
  169. ListViewItem listItem = sender as ListViewItem;
  170. if (listItem != null)
  171. {
  172. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  173. viewModel.LostFocusCommand.Execute(listItem);
  174. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  175. TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  176. if (textBlock.Visibility != Visibility.Visible)
  177. {
  178. if (isAdd == false && isRename == false)
  179. {
  180. listItem.IsSelected = false;
  181. }
  182. else
  183. {
  184. listItem.IsSelected = true;
  185. isAdd = false;
  186. }
  187. //if (text.IsSelectionActive == true)
  188. //{
  189. // textBlock.Visibility = Visibility.Visible;
  190. //}
  191. }
  192. else
  193. {
  194. listItem.IsSelected = true;
  195. }
  196. //if (histotyListBoxItem != null)
  197. //{
  198. // histotyListBoxItem.IsSelected = false;
  199. // TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  200. // textBlock.Visibility = Visibility.Visible;
  201. // textBlock.Focusable = true;
  202. //}
  203. //if (textBlock.Visibility == Visibility.Collapsed)
  204. //{
  205. // listItem.IsSelected = false;
  206. // //textBlock.Visibility = Visibility.Visible;
  207. //}
  208. //else
  209. //{
  210. // listItem.IsSelected = true;
  211. // //textBlock.Visibility = Visibility.Collapsed;
  212. //}
  213. }
  214. }
  215. private bool isRename = false;
  216. /// <summary>
  217. /// 右键菜单-重命名
  218. /// </summary>
  219. /// <param name="sender"></param>
  220. /// <param name="e"></param>
  221. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  222. {
  223. if (sender is MenuItem)
  224. {
  225. MenuItem menuItem = (MenuItem)sender;
  226. CPDFBookmark bookmark = menuItem.CommandParameter as CPDFBookmark;
  227. if (bookmark != null)
  228. {
  229. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  230. isRename = true;
  231. ListViewItem listBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  232. histotyListBoxItem = listBoxItem;
  233. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  234. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  235. SetSelectedStatus(listBoxItem, textBox, textBlock);
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// BookMarkListView,鼠标点击
  241. /// </summary>
  242. /// <param name="sender"></param>
  243. /// <param name="e"></param>
  244. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  245. {
  246. if (e.LeftButton == MouseButtonState.Pressed)
  247. {
  248. var pos = e.GetPosition(BookMarkListView);
  249. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  250. if (result != null)
  251. {
  252. //获取当前鼠标指针下的容器
  253. var listBoxItem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  254. if (listBoxItem == null)
  255. {
  256. if (BookMarkListView.SelectedItem != null)
  257. {
  258. ListViewItem item = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  259. item.IsSelected = false;
  260. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  261. box.Visibility = Visibility.Visible;
  262. BookMarkListView.SelectedItems.Clear();
  263. }
  264. if (histotyListBoxItem != null)
  265. {
  266. var pos1 = e.GetPosition(histotyListBoxItem);
  267. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  268. if (result1 == null)
  269. {
  270. histotyListBoxItem.IsSelected = false;
  271. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  272. textBlock.Visibility = Visibility.Visible;
  273. textBlock.Focusable = true;
  274. }
  275. }
  276. }
  277. else
  278. {
  279. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  280. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  281. if (textBox.IsFocused == false)
  282. {
  283. listBoxItem.Focus();
  284. }
  285. }
  286. }
  287. //BookMarkListView.Focus();
  288. }
  289. }
  290. /// <summary>
  291. /// 右键菜单-删除
  292. /// </summary>
  293. /// <param name="sender"></param>
  294. /// <param name="e"></param>
  295. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  296. {
  297. List<int> pagelist = new List<int>();
  298. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  299. {
  300. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  301. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  302. }
  303. pagelist.Sort();
  304. for (int i = 0; i < pagelist.Count; i++)
  305. {
  306. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  307. if (data != null)
  308. {
  309. viewModel.DeleteCommand.Execute(data);
  310. }
  311. }
  312. }
  313. public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
  314. {
  315. isAdd = true;
  316. viewModel.AddBookmarkCommand.Execute(BookMarkListView);
  317. if (viewModel.IsHasBookmark == false)
  318. {
  319. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  320. ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  321. if (myListBoxItem == null)
  322. {
  323. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  324. BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
  325. }
  326. else
  327. {
  328. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  329. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  330. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  331. }
  332. }
  333. }
  334. private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  335. {
  336. ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
  337. if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
  338. {
  339. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  340. ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  341. if (myListBoxItem != null)
  342. {
  343. if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
  344. {
  345. BookMarkListView.UpdateLayout();
  346. myListBoxItem.UpdateLayout();
  347. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  348. }
  349. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  350. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  351. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  352. }
  353. }
  354. }
  355. private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
  356. {
  357. if (BookMarkListView.SelectedItems == null)
  358. {
  359. return;
  360. }
  361. if (e.Key == Key.Escape)
  362. {
  363. BookMarkListView.SelectedItems.Clear();
  364. }
  365. //if (e.Key == Key.Delete)
  366. //{
  367. // if (BookMarkListView.SelectedItems.Count > 0)
  368. // {
  369. // //全选删除
  370. // if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
  371. // {
  372. // }
  373. // }
  374. //}
  375. }
  376. }
  377. }