BookmarkContent.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. ListViewItem listItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  50. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  51. if (textBlock == null)
  52. return;
  53. TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  54. if (textBlock.Visibility == Visibility.Collapsed)
  55. {
  56. textBlock.Visibility = Visibility.Visible;
  57. }
  58. BookMarkListView.SelectedItems.Clear();
  59. }
  60. /// <summary>
  61. ///ListViewItem,鼠标左键点击
  62. /// </summary>
  63. /// <param name="sender"></param>
  64. /// <param name="e"></param>
  65. private void ListViewItem_MouseButtonDown(object sender, MouseButtonEventArgs e)
  66. {
  67. object[] objects = new object[] { sender, e };
  68. TextBlock textBlock = null;
  69. ListViewItem listBoxItem = (sender as ListViewItem);
  70. if (e.LeftButton == MouseButtonState.Pressed)
  71. {
  72. if (e.ClickCount >= 2)
  73. {
  74. histotyListBoxItem = listBoxItem;
  75. if (e.OriginalSource is TextBlock)
  76. {
  77. textBlock = (TextBlock)e.OriginalSource;
  78. if (textBlock != null)
  79. {
  80. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  81. SetSelectedStatus(listBoxItem, textBox, textBlock);
  82. }
  83. }
  84. }
  85. else if (e.ClickCount == 1)
  86. {
  87. if (histotyListBoxItem != listBoxItem)
  88. {
  89. if (histotyListBoxItem != null)
  90. {
  91. histotyListBoxItem.IsSelected = false;
  92. textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  93. textBlock.Visibility = Visibility.Visible;
  94. textBlock.Focusable = true;
  95. }
  96. }
  97. viewModel.ListViewItemMouseDownCommand.Execute(sender);
  98. }
  99. }
  100. else if (e.RightButton == MouseButtonState.Pressed)
  101. {
  102. ContextMenu contextMenu = listBoxItem.ContextMenu;
  103. if (contextMenu.Items.Count == 3)
  104. {
  105. MenuItem rename = contextMenu.Items[0] as MenuItem;
  106. MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
  107. MenuItem del = contextMenu.Items[2] as MenuItem;
  108. if (isSelects)
  109. {
  110. rename.IsEnabled = false;
  111. editPageIndex.IsEnabled = false;
  112. isSelects = false;
  113. }
  114. else
  115. {
  116. CPDFBookmark bookmark = (CPDFBookmark)listBoxItem.DataContext;
  117. if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
  118. {
  119. editPageIndex.IsEnabled = false;
  120. return;
  121. }
  122. rename.IsEnabled = true;
  123. editPageIndex.IsEnabled = true;
  124. editPageIndex.CommandParameter = listBoxItem.DataContext;
  125. editPageIndex.Command = viewModel.EditPageIndexCommand;
  126. }
  127. }
  128. }
  129. }
  130. private bool isSelects = false;
  131. private void BookMarkListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  132. {
  133. var a1 = (sender as ListView).SelectedItems.Count;
  134. if (a1 > 1)
  135. {
  136. isSelects = true;
  137. }
  138. }
  139. /// <summary>
  140. /// ListViewItem双击时选中状态
  141. /// </summary>
  142. /// <param name="listBoxItem"></param>
  143. /// <param name="textBox"></param>
  144. /// <param name="textBlock"></param>
  145. private void SetSelectedStatus(ListBoxItem listBoxItem, TextBox textBox, TextBlock textBlock)
  146. {
  147. listBoxItem.IsSelected = true;
  148. listBoxItem.Focus();
  149. textBlock.Visibility = Visibility.Collapsed;
  150. isRename = true;
  151. textBox.Dispatcher.BeginInvoke(new Action(() =>
  152. {
  153. textBox.Focus();
  154. textBox.SelectAll();
  155. //listBoxItem.IsSelected = true;
  156. }));
  157. }
  158. /// <summary>
  159. /// ListViewItem失去焦点
  160. /// </summary>
  161. /// <param name="sender"></param>
  162. /// <param name="e"></param>
  163. private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
  164. {
  165. ListViewItem listItem = sender as ListViewItem;
  166. if (listItem != null)
  167. {
  168. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  169. viewModel.LostFocusCommand.Execute(listItem);
  170. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  171. TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  172. if (textBlock.Visibility != Visibility.Visible)
  173. {
  174. if (isAdd == false && isRename == false)
  175. {
  176. listItem.IsSelected = false;
  177. }
  178. else
  179. {
  180. listItem.IsSelected = true;
  181. isAdd = false;
  182. }
  183. //if (text.IsSelectionActive == true)
  184. //{
  185. // textBlock.Visibility = Visibility.Visible;
  186. //}
  187. }
  188. else
  189. {
  190. listItem.IsSelected = true;
  191. }
  192. //if (histotyListBoxItem != null)
  193. //{
  194. // histotyListBoxItem.IsSelected = false;
  195. // TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  196. // textBlock.Visibility = Visibility.Visible;
  197. // textBlock.Focusable = true;
  198. //}
  199. //if (textBlock.Visibility == Visibility.Collapsed)
  200. //{
  201. // listItem.IsSelected = false;
  202. // //textBlock.Visibility = Visibility.Visible;
  203. //}
  204. //else
  205. //{
  206. // listItem.IsSelected = true;
  207. // //textBlock.Visibility = Visibility.Collapsed;
  208. //}
  209. }
  210. }
  211. private bool isRename = false;
  212. /// <summary>
  213. /// 右键菜单-重命名
  214. /// </summary>
  215. /// <param name="sender"></param>
  216. /// <param name="e"></param>
  217. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  218. {
  219. if (sender is MenuItem)
  220. {
  221. MenuItem menuItem = (MenuItem)sender;
  222. CPDFBookmark bookmark = menuItem.CommandParameter as CPDFBookmark;
  223. if (bookmark != null)
  224. {
  225. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  226. isRename = true;
  227. ListViewItem listBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  228. histotyListBoxItem = listBoxItem;
  229. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  230. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  231. SetSelectedStatus(listBoxItem, textBox, textBlock);
  232. }
  233. }
  234. }
  235. /// <summary>
  236. /// BookMarkListView,鼠标点击
  237. /// </summary>
  238. /// <param name="sender"></param>
  239. /// <param name="e"></param>
  240. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  241. {
  242. if (e.LeftButton == MouseButtonState.Pressed)
  243. {
  244. var pos = e.GetPosition(BookMarkListView);
  245. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  246. if (result != null)
  247. {
  248. //获取当前鼠标指针下的容器
  249. var listBoxItem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  250. if (listBoxItem == null)
  251. {
  252. if (BookMarkListView.SelectedItem != null)
  253. {
  254. ListViewItem item = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  255. item.IsSelected = false;
  256. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  257. box.Visibility = Visibility.Visible;
  258. BookMarkListView.SelectedItems.Clear();
  259. }
  260. if (histotyListBoxItem != null)
  261. {
  262. var pos1 = e.GetPosition(histotyListBoxItem);
  263. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  264. if (result1 == null)
  265. {
  266. histotyListBoxItem.IsSelected = false;
  267. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  268. textBlock.Visibility = Visibility.Visible;
  269. textBlock.Focusable = true;
  270. }
  271. }
  272. }
  273. else
  274. {
  275. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  276. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  277. if (textBox.IsFocused == false)
  278. {
  279. listBoxItem.Focus();
  280. }
  281. }
  282. }
  283. //BookMarkListView.Focus();
  284. }
  285. }
  286. /// <summary>
  287. /// 右键菜单-删除
  288. /// </summary>
  289. /// <param name="sender"></param>
  290. /// <param name="e"></param>
  291. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  292. {
  293. List<int> pagelist = new List<int>();
  294. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  295. {
  296. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  297. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  298. }
  299. pagelist.Sort();
  300. for (int i = 0; i < pagelist.Count; i++)
  301. {
  302. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  303. if (data != null)
  304. {
  305. viewModel.DeleteCommand.Execute(data);
  306. }
  307. }
  308. }
  309. public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
  310. {
  311. isAdd = true;
  312. viewModel.AddBookmarkCommand.Execute(BookMarkListView);
  313. if (viewModel.IsHasBookmark == false)
  314. {
  315. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  316. ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  317. if (myListBoxItem == null)
  318. {
  319. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  320. BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
  321. }
  322. else
  323. {
  324. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  325. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  326. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  327. }
  328. }
  329. }
  330. private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  331. {
  332. ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
  333. if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
  334. {
  335. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  336. ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  337. if (myListBoxItem != null)
  338. {
  339. if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
  340. {
  341. BookMarkListView.UpdateLayout();
  342. myListBoxItem.UpdateLayout();
  343. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  344. }
  345. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  346. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  347. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  348. }
  349. }
  350. }
  351. private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
  352. {
  353. if (BookMarkListView.SelectedItems == null)
  354. {
  355. return;
  356. }
  357. if (e.Key == Key.Escape)
  358. {
  359. BookMarkListView.SelectedItems.Clear();
  360. }
  361. //if (e.Key == Key.Delete)
  362. //{
  363. // if (BookMarkListView.SelectedItems.Count > 0)
  364. // {
  365. // //全选删除
  366. // if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
  367. // {
  368. // }
  369. // }
  370. //}
  371. }
  372. }
  373. }