BookmarkContent.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. 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.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. }
  187. else
  188. {
  189. listItem.IsSelected = true;
  190. isAdd = false;
  191. }
  192. //if (text.IsSelectionActive == true)
  193. //{
  194. // textBlock.Visibility = Visibility.Visible;
  195. //}
  196. }
  197. else
  198. {
  199. listItem.IsSelected = true;
  200. }
  201. //if (histotyListBoxItem != null)
  202. //{
  203. // histotyListBoxItem.IsSelected = false;
  204. // TextBlock textBlock1 = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  205. // textBlock1.Visibility = Visibility.Visible;
  206. // textBlock1.Focusable = true;
  207. //}
  208. //TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  209. //TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  210. //if (textBlock.Visibility == Visibility.Collapsed)
  211. //{
  212. // listItem.IsSelected = false;
  213. // //textBlock.Visibility = Visibility.Visible;
  214. //}
  215. //else
  216. //{
  217. // listItem.IsSelected = true;
  218. // //textBlock.Visibility = Visibility.Collapsed;
  219. //}
  220. }
  221. }
  222. private bool isRename = false;
  223. /// <summary>
  224. /// 右键菜单-重命名
  225. /// </summary>
  226. /// <param name="sender"></param>
  227. /// <param name="e"></param>
  228. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  229. {
  230. if (sender is MenuItem)
  231. {
  232. MenuItem menuItem = (MenuItem)sender;
  233. CPDFBookmark bookmark = menuItem.CommandParameter as CPDFBookmark;
  234. if (bookmark != null)
  235. {
  236. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  237. isRename = true;
  238. ListViewItem listBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  239. histotyListBoxItem = listBoxItem;
  240. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  241. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  242. SetSelectedStatus(listBoxItem, textBox, textBlock);
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// BookMarkListView,鼠标点击
  248. /// </summary>
  249. /// <param name="sender"></param>
  250. /// <param name="e"></param>
  251. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  252. {
  253. if (e.LeftButton == MouseButtonState.Pressed)
  254. {
  255. var pos = e.GetPosition(BookMarkListView);
  256. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  257. if (result != null)
  258. {
  259. //获取当前鼠标指针下的容器
  260. var listBoxItem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  261. if (listBoxItem == null)
  262. {
  263. if (BookMarkListView.SelectedItem != null)
  264. {
  265. ListViewItem item = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  266. item.IsSelected = false;
  267. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  268. box.Visibility = Visibility.Visible;
  269. BookMarkListView.SelectedItems.Clear();
  270. }
  271. if (histotyListBoxItem != null)
  272. {
  273. var pos1 = e.GetPosition(histotyListBoxItem);
  274. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  275. if (result1 == null)
  276. {
  277. histotyListBoxItem.IsSelected = false;
  278. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  279. textBlock.Visibility = Visibility.Visible;
  280. textBlock.Focusable = true;
  281. }
  282. }
  283. }
  284. else
  285. {
  286. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  287. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  288. if (textBox.IsFocused == false)
  289. {
  290. listBoxItem.Focus();
  291. }
  292. }
  293. }
  294. //BookMarkListView.Focus();
  295. }
  296. }
  297. /// <summary>
  298. /// 右键菜单-删除
  299. /// </summary>
  300. /// <param name="sender"></param>
  301. /// <param name="e"></param>
  302. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  303. {
  304. List<int> pagelist = new List<int>();
  305. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  306. {
  307. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  308. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  309. }
  310. pagelist.Sort();
  311. for (int i = 0; i < pagelist.Count; i++)
  312. {
  313. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  314. if (data != null)
  315. {
  316. viewModel.DeleteCommand.Execute(data);
  317. }
  318. }
  319. }
  320. public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
  321. {
  322. isAdd = true;
  323. viewModel.AddBookmarkCommand.Execute(BookMarkListView);
  324. if (viewModel.IsHasBookmark == false)
  325. {
  326. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  327. ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  328. if (myListBoxItem == null)
  329. {
  330. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  331. BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
  332. }
  333. else
  334. {
  335. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  336. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  337. histotyListBoxItem = myListBoxItem;
  338. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  339. }
  340. }
  341. }
  342. private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  343. {
  344. ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
  345. if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
  346. {
  347. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  348. ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  349. if (myListBoxItem != null)
  350. {
  351. if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
  352. {
  353. BookMarkListView.UpdateLayout();
  354. myListBoxItem.UpdateLayout();
  355. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  356. }
  357. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  358. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  359. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  360. }
  361. }
  362. }
  363. private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
  364. {
  365. if (BookMarkListView.SelectedItems == null)
  366. {
  367. return;
  368. }
  369. if (e.Key == Key.Escape)
  370. {
  371. BookMarkListView.SelectedItems.Clear();
  372. }
  373. //if (e.Key == Key.Delete)
  374. //{
  375. // if (BookMarkListView.SelectedItems.Count > 0)
  376. // {
  377. // //全选删除
  378. // if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
  379. // {
  380. // }
  381. // }
  382. //}
  383. }
  384. }
  385. }