BookmarkContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using ImTools;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum;
  19. using static System.Net.Mime.MediaTypeNames;
  20. namespace PDF_Office.ViewModels.BOTA
  21. {
  22. public class BookmarkContentViewModel : BindableBase, INavigationAware
  23. {
  24. #region 属性
  25. private static string hoverColor = "#D9D9D9";
  26. private static string defaultColor = "#F2F2F2";
  27. private static string selectColcr = "#97D7FB";
  28. private static string borderBackground = "#FFFFFF";
  29. private static string borderBrush = "#000000";
  30. private IRegionManager region;
  31. private IDialogService dialogs;
  32. public CPDFViewer PDFViewer;
  33. //public ObservableCollection<CPDFBookmark> Bookmarklist { get; set; }
  34. private ObservableCollection<CPDFBookmark> list;
  35. private ObservableCollection<CPDFBookmark> bookmarklist;
  36. public ObservableCollection<CPDFBookmark> Bookmarklist
  37. {
  38. get
  39. {
  40. return bookmarklist;
  41. }
  42. set
  43. {
  44. SetProperty(ref bookmarklist, value);
  45. }
  46. }
  47. private Visibility isEmpty;
  48. public Visibility IsEmptyPanelVisibility
  49. {
  50. get
  51. {
  52. return isEmpty;
  53. }
  54. set
  55. {
  56. SetProperty(ref isEmpty, value);
  57. }
  58. }
  59. #endregion 属性
  60. #region 命令
  61. public DelegateCommand<object> MouseClickCommand { get; set; }
  62. public DelegateCommand<object> MouseEnterCommand { get; set; }
  63. public DelegateCommand<object> MouseMoveCommand { get; set; }
  64. public DelegateCommand<object> LostFocusCommand { get; set; }
  65. public DelegateCommand<object> GotFocusCommand { get; set; }
  66. public DelegateCommand<object> AddBookmarkCommand { get; set; }
  67. #endregion 命令
  68. public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  69. {
  70. region = regionManager;
  71. dialogs = dialogService;
  72. MouseClickCommand = new DelegateCommand<object>(MouseClickEvent);
  73. MouseEnterCommand = new DelegateCommand<object>(MouseEnterEvent);
  74. LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
  75. MouseMoveCommand = new DelegateCommand<object>(MouseMoveEvent);
  76. GotFocusCommand = new DelegateCommand<object>(GotFocusEvent);
  77. AddBookmarkCommand = new DelegateCommand<object>(AddBookmarkEvent);
  78. }
  79. private void AddBookmarkEvent(object obj)
  80. {
  81. int index = PDFViewer.CurrentIndex;
  82. string mark = string.Format($"第{index}页");
  83. }
  84. private void GotFocusEvent(object obj)
  85. {
  86. if (obj is RoutedEventArgs)
  87. {
  88. var mouse = (RoutedEventArgs)obj;
  89. if (mouse.Source is TextBox)
  90. {
  91. TextBox textBox = (TextBox)mouse.Source;
  92. textBox.Select(textBox.Text.Length, 0);
  93. textBox.SelectAll();
  94. }
  95. }
  96. }
  97. private void MouseMoveEvent(object obj)
  98. {
  99. //if (obj is MouseEventArgs)
  100. //{
  101. // var mouse = (MouseEventArgs)obj;
  102. // Grid grid = null;
  103. // if (mouse.Source is TextBox)
  104. // {
  105. // TextBox text = (TextBox)mouse.Source;
  106. // if (text != null)
  107. // {
  108. // text.Visibility = Visibility.Collapsed;
  109. // grid = (text.Parent as StackPanel).Parent as Grid;
  110. // grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
  111. // }
  112. // }
  113. //}
  114. }
  115. private void LostFocusEvent(object obj)
  116. {
  117. if (obj is RoutedEventArgs)
  118. {
  119. var mouse = (RoutedEventArgs)obj;
  120. Grid grid = null;
  121. if (mouse.Source is Grid)
  122. {
  123. grid = mouse.Source as Grid;
  124. if (grid != null)
  125. {
  126. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(grid);
  127. if (box != null)
  128. {
  129. box.Visibility = Visibility.Visible;
  130. }
  131. grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
  132. }
  133. }
  134. if (mouse.Source is TextBox)
  135. {
  136. TextBox textBox = (TextBox)mouse.Source;
  137. if (textBox != null)
  138. {
  139. //textBox.Visibility = Visibility.Collapsed;
  140. grid = (textBox.Parent as StackPanel).Parent as Grid;
  141. grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
  142. }
  143. }
  144. }
  145. }
  146. public void MouseEnterEvent(object obj)
  147. {
  148. if (obj is MouseEventArgs)
  149. {
  150. var mouse = (MouseEventArgs)obj;
  151. Grid grid = null;
  152. if (mouse.Source is Grid)
  153. {
  154. grid = mouse.Source as Grid;
  155. if (grid != null)
  156. {
  157. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(grid);
  158. if (box != null)
  159. {
  160. //box.Visibility = Visibility.Visible;
  161. }
  162. grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
  163. }
  164. }
  165. if (mouse.Source is TextBlock)
  166. {
  167. TextBlock text = (TextBlock)mouse.Source;
  168. if (text != null)
  169. {
  170. //text.Visibility = Visibility.Visible;
  171. grid = (text.Parent as StackPanel).Parent as Grid;
  172. grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
  173. }
  174. }
  175. //if (mouse.Source is TextBox)
  176. //{
  177. // TextBox text = (TextBox)mouse.Source;
  178. // if (text != null)
  179. // {
  180. // text.Visibility = Visibility.Collapsed;
  181. // grid = (text.Parent as StackPanel).Parent as Grid;
  182. // grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
  183. // }
  184. //}
  185. }
  186. else
  187. {
  188. }
  189. }
  190. public void MouseClickEvent(object obj)
  191. {
  192. if (obj is MouseButtonEventArgs)
  193. {
  194. var mouse = (MouseButtonEventArgs)obj;
  195. if (mouse.LeftButton == MouseButtonState.Pressed)
  196. {
  197. TextBlock box = null;
  198. Grid grid = null;
  199. if (mouse.ClickCount >= 2)
  200. {
  201. if (mouse.Source is Grid)
  202. {
  203. grid = mouse.Source as Grid;
  204. //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  205. if (grid != null)
  206. {
  207. box = CommonHelper.FindVisualChild<TextBlock>(grid);
  208. grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  209. if (box != null)
  210. {
  211. box.Visibility = Visibility.Collapsed;
  212. //TextBox textBox = CommonHelper.FindVisualChild<TextBox>((box.Parent as StackPanel));
  213. //FocusManager.SetFocusedElement(box.Parent, textBox);
  214. //Keyboard.Focus(textBox);
  215. //textBox.IsHitTestVisible = true;
  216. //textBox.Focus();
  217. }
  218. }
  219. }
  220. if (mouse.Source is TextBlock)
  221. {
  222. box = (TextBlock)mouse.Source;
  223. grid = (box.Parent as StackPanel).Parent as Grid;
  224. grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
  225. if (box != null)
  226. {
  227. box.Visibility = Visibility.Collapsed;
  228. //TextBox textBox = CommonHelper.FindVisualChild<TextBox>((box.Parent as StackPanel));
  229. //FocusManager.SetFocusedElement(box.Parent, textBox);
  230. //Keyboard.Focus(textBox);
  231. //textBox.IsHitTestVisible = true;
  232. //textBox.Focus();
  233. }
  234. }
  235. }
  236. //MessageBox.Show("dsafa");
  237. }
  238. }
  239. }
  240. public void OnNavigatedTo(NavigationContext navigationContext)
  241. {
  242. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  243. if (PDFViewer == null)
  244. {
  245. return;
  246. }
  247. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
  248. if (Bookmarklist.Count < 1)
  249. {
  250. IsEmptyPanelVisibility = Visibility.Visible;
  251. return;
  252. }
  253. else
  254. {
  255. IsEmptyPanelVisibility = Visibility.Hidden;
  256. return;
  257. }
  258. }
  259. public bool IsNavigationTarget(NavigationContext navigationContext)
  260. {
  261. return true;
  262. }
  263. public void OnNavigatedFrom(NavigationContext navigationContext)
  264. {
  265. }
  266. }
  267. }