BookmarkContent.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace PDF_Office.Views.BOTA
  19. {
  20. /// <summary>
  21. /// BookmarkContent.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class BookmarkContent : UserControl
  24. {
  25. private BookmarkContentViewModel viewModel = null;
  26. /// <summary>
  27. /// 上一个ListBoxItem,为选中状态做准备
  28. /// </summary>
  29. private ListBoxItem histotyListBoxItem = null;
  30. public BookmarkContent()
  31. {
  32. InitializeComponent();
  33. viewModel = this.DataContext as BookmarkContentViewModel;
  34. }
  35. /// <summary>
  36. ///ListViewItem,鼠标左键点击
  37. /// </summary>
  38. /// <param name="sender"></param>
  39. /// <param name="e"></param>
  40. private void ListViewItem_MouseButtonDown(object sender, MouseButtonEventArgs e)
  41. {
  42. object[] objects = new object[] { sender, e };
  43. TextBlock textBlock = null;
  44. ListBoxItem listBoxItem = (sender as ListBoxItem);
  45. if (e.LeftButton == MouseButtonState.Pressed)
  46. {
  47. if (e.ClickCount >= 2)
  48. {
  49. histotyListBoxItem = listBoxItem;
  50. if (e.OriginalSource is TextBlock)
  51. {
  52. textBlock = (TextBlock)e.OriginalSource;
  53. if (textBlock != null)
  54. {
  55. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  56. SetSelectedStatus(listBoxItem, textBox, textBlock);
  57. }
  58. }
  59. }
  60. else if (e.ClickCount == 1)
  61. {
  62. if (histotyListBoxItem != listBoxItem)
  63. {
  64. if (histotyListBoxItem != null)
  65. {
  66. histotyListBoxItem.IsSelected = false;
  67. textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  68. textBlock.Visibility = Visibility.Visible;
  69. textBlock.Focusable = true;
  70. }
  71. }
  72. viewModel.ListViewItemMouseDownCommand.Execute(sender);
  73. }
  74. }
  75. else if (e.RightButton == MouseButtonState.Pressed)
  76. {
  77. ContextMenu contextMenu = listBoxItem.ContextMenu;
  78. if (contextMenu.Items.Count == 3)
  79. {
  80. MenuItem rename = contextMenu.Items[0] as MenuItem;
  81. MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
  82. MenuItem del = contextMenu.Items[2] as MenuItem;
  83. if (BookMarkListView.SelectedItems.Count > 1)
  84. {
  85. rename.IsEnabled = false;
  86. editPageIndex.IsEnabled = false;
  87. }
  88. else
  89. {
  90. rename.IsEnabled = true;
  91. editPageIndex.IsEnabled = true;
  92. editPageIndex.Command = viewModel.EditPageIndexCommand;
  93. }
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// ListViewItem双击时选中状态
  99. /// </summary>
  100. /// <param name="listBoxItem"></param>
  101. /// <param name="textBox"></param>
  102. /// <param name="textBlock"></param>
  103. private void SetSelectedStatus(ListBoxItem listBoxItem, TextBox textBox, TextBlock textBlock)
  104. {
  105. listBoxItem.IsSelected = true;
  106. listBoxItem.Focus();
  107. textBlock.Visibility = Visibility.Collapsed;
  108. textBox.Dispatcher.BeginInvoke(new Action(() =>
  109. {
  110. textBox.Focus();
  111. textBox.SelectAll();
  112. }));
  113. }
  114. /// <summary>
  115. /// ListViewItem失去焦点
  116. /// </summary>
  117. /// <param name="sender"></param>
  118. /// <param name="e"></param>
  119. private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
  120. {
  121. ListBoxItem listItem = sender as ListBoxItem;
  122. if (listItem != null)
  123. {
  124. viewModel.LostFocusCommand.Execute(listItem);
  125. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  126. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listItem);
  127. if (textBlock.Visibility == Visibility.Collapsed)
  128. {
  129. listItem.IsSelected = false;
  130. textBlock.Visibility = Visibility.Visible;
  131. }
  132. else
  133. {
  134. listItem.IsSelected = true;
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// 右键菜单-重命名
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  144. {
  145. if (sender is MenuItem)
  146. {
  147. MenuItem menuItem = (MenuItem)sender;
  148. CPDFBookmark bookmark = menuItem.CommandParameter as CPDFBookmark;
  149. if (bookmark != null)
  150. {
  151. ListBoxItem listBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  152. histotyListBoxItem = listBoxItem;
  153. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  154. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  155. SetSelectedStatus(listBoxItem, textBox, textBlock);
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// BookMarkListView,鼠标点击
  161. /// </summary>
  162. /// <param name="sender"></param>
  163. /// <param name="e"></param>
  164. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  165. {
  166. if (e.LeftButton == MouseButtonState.Pressed)
  167. {
  168. var pos = e.GetPosition(BookMarkListView);
  169. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  170. if (result != null)
  171. {
  172. //获取当前鼠标指针下的容器
  173. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  174. if (listBoxItem == null)
  175. {
  176. if (BookMarkListView.SelectedItem != null)
  177. {
  178. ListBoxItem item = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  179. item.IsSelected = false;
  180. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  181. box.Visibility = Visibility.Visible;
  182. BookMarkListView.SelectedItems.Clear();
  183. }
  184. if (histotyListBoxItem != null)
  185. {
  186. var pos1 = e.GetPosition(histotyListBoxItem);
  187. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  188. if (result1 == null)
  189. {
  190. histotyListBoxItem.IsSelected = false;
  191. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  192. textBlock.Visibility = Visibility.Visible;
  193. textBlock.Focusable = true;
  194. }
  195. }
  196. }
  197. else
  198. {
  199. listBoxItem.Focus();
  200. }
  201. }
  202. BookMarkListView.Focus();
  203. }
  204. }
  205. /// <summary>
  206. /// 右键菜单-删除
  207. /// </summary>
  208. /// <param name="sender"></param>
  209. /// <param name="e"></param>
  210. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  211. {
  212. List<int> pagelist = new List<int>();
  213. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  214. {
  215. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  216. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  217. }
  218. pagelist.Sort();
  219. for (int i = 0; i < pagelist.Count; i++)
  220. {
  221. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  222. if (data != null)
  223. {
  224. viewModel.DeleteCommand.Execute(data);
  225. }
  226. }
  227. }
  228. }
  229. }