BookmarkContent.xaml.cs 16 KB

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