BookmarkContent.xaml.cs 22 KB

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