BookmarkContent.xaml.cs 16 KB

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