BookmarkContent.xaml.cs 9.1 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 textBox = CommonHelper.FindVisualChild<TextBlock>(listItem);
  126. if (textBox.Visibility == Visibility.Collapsed)
  127. {
  128. listItem.IsSelected = true;
  129. }
  130. else
  131. {
  132. listItem.IsSelected = false;
  133. }
  134. }
  135. }
  136. /// <summary>
  137. /// 右键菜单-重命名
  138. /// </summary>
  139. /// <param name="sender"></param>
  140. /// <param name="e"></param>
  141. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  142. {
  143. if (sender is MenuItem)
  144. {
  145. MenuItem menuItem = (MenuItem)sender;
  146. CPDFBookmark bookmark = menuItem.CommandParameter as CPDFBookmark;
  147. if (bookmark != null)
  148. {
  149. ListBoxItem listBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  150. histotyListBoxItem = listBoxItem;
  151. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  152. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  153. SetSelectedStatus(listBoxItem, textBox, textBlock);
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// BookMarkListView,鼠标点击
  159. /// </summary>
  160. /// <param name="sender"></param>
  161. /// <param name="e"></param>
  162. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  163. {
  164. if (e.LeftButton == MouseButtonState.Pressed)
  165. {
  166. var pos = e.GetPosition(BookMarkListView);
  167. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  168. if (result != null)
  169. {
  170. //获取当前鼠标指针下的容器
  171. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  172. if (listBoxItem == null)
  173. {
  174. if (BookMarkListView.SelectedItem != null)
  175. {
  176. ListBoxItem item = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  177. item.IsSelected = false;
  178. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  179. box.Visibility = Visibility.Visible;
  180. BookMarkListView.SelectedItems.Clear();
  181. }
  182. if (histotyListBoxItem != null)
  183. {
  184. var pos1 = e.GetPosition(histotyListBoxItem);
  185. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  186. if (result1 == null)
  187. {
  188. histotyListBoxItem.IsSelected = false;
  189. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  190. textBlock.Visibility = Visibility.Visible;
  191. textBlock.Focusable = true;
  192. }
  193. }
  194. }
  195. else
  196. {
  197. listBoxItem.Focus();
  198. }
  199. }
  200. BookMarkListView.Focus();
  201. }
  202. }
  203. /// <summary>
  204. /// 右键菜单-删除
  205. /// </summary>
  206. /// <param name="sender"></param>
  207. /// <param name="e"></param>
  208. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  209. {
  210. List<int> pagelist = new List<int>();
  211. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  212. {
  213. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  214. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  215. }
  216. pagelist.Sort();
  217. for (int i = 0; i < pagelist.Count; i++)
  218. {
  219. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  220. if (data != null)
  221. {
  222. viewModel.DeleteCommand.Execute(data);
  223. }
  224. }
  225. }
  226. }
  227. }