RecentFilesContent.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Win32;
  3. using PDF_Office.CustomControl;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Properties;
  6. using PDF_Office.ViewModels.HomePanel.RecentFiles;
  7. using PDFSettings.Settings;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Input;
  17. using System.Windows.Interop;
  18. using System.Windows.Navigation;
  19. using winform = System.Windows.Forms;
  20. namespace PDF_Office.Views.HomePanel.RecentFiles
  21. {
  22. public partial class RecentFilesContent : UserControl
  23. {
  24. private RecentFilesContentViewModel ViewModel => DataContext as RecentFilesContentViewModel;
  25. public RecentFilesContent()
  26. {
  27. InitializeComponent();
  28. }
  29. #region 初始化
  30. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  31. {
  32. InitVariables();
  33. InitEvents();
  34. }
  35. private void InitVariables()
  36. {
  37. if (RecentFilesList.SelectedItems != null)
  38. RecentFilesList.SelectedItems.Clear();
  39. if (GridRecentFilesList.SelectedItems != null)
  40. GridRecentFilesList.SelectedItems.Clear();
  41. }
  42. private void InitEvents()
  43. {
  44. ViewModel.RecentFilesSelectionHandler -= ViewModel_RecentFilesSelection;
  45. ViewModel.RecentFilesSelectionHandler += ViewModel_RecentFilesSelection;
  46. }
  47. #endregion
  48. #region UI操作
  49. private void ViewModel_RecentFilesSelection(object sender, bool e)
  50. {
  51. if (e)
  52. {
  53. RecentFilesList.SelectedItems.Clear();
  54. if (GridRecentFilesList.SelectedItems.Count > 0)
  55. {
  56. for (int i = 0; i < GridRecentFilesList.SelectedItems.Count; i++)
  57. {
  58. RecentFilesList.SelectedItems.Add(GridRecentFilesList.SelectedItems[i]);
  59. }
  60. }
  61. }
  62. else
  63. {
  64. GridRecentFilesList.SelectedItems.Clear();
  65. if (RecentFilesList.SelectedItems.Count > 0)
  66. {
  67. for (int i = 0; i < RecentFilesList.SelectedItems.Count; i++)
  68. {
  69. GridRecentFilesList.SelectedItems.Add(RecentFilesList.SelectedItems[i]);
  70. }
  71. }
  72. }
  73. }
  74. private void ContextMenu_Opened(object sender, RoutedEventArgs e)
  75. {
  76. try
  77. {
  78. ListView listView;
  79. if (BtnListMode.IsChecked == true)
  80. listView = RecentFilesList;
  81. else
  82. listView = GridRecentFilesList;
  83. var contexMenu = sender as ContextMenu;
  84. if (contexMenu != null)
  85. {
  86. foreach (var item in contexMenu.Items)
  87. {
  88. var menuitem = item as MenuItem;
  89. menuitem.Visibility = Visibility.Visible;
  90. }
  91. }
  92. if (listView.SelectedItems.Count >= 2)
  93. {
  94. foreach (var item in contexMenu.Items)
  95. {
  96. var menuitem = item as MenuItem;
  97. if (menuitem.Name == "FilePathMenuItem")
  98. menuitem.Visibility = Visibility.Collapsed;
  99. }
  100. }
  101. }
  102. catch { }
  103. }
  104. /// <summary>
  105. /// 禁用触发鼠标右键,列表项被选中
  106. /// </summary>
  107. private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  108. {
  109. Trace.WriteLine("Preview MouseRightButtonDown");
  110. e.Handled = true;
  111. }
  112. #endregion
  113. #region Events
  114. /// <summary>
  115. /// 右键菜单:删除文件记录
  116. /// </summary>
  117. private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
  118. {
  119. ListView listView;
  120. if (BtnListMode.IsChecked == true)
  121. listView = RecentFilesList;
  122. else
  123. listView = GridRecentFilesList;
  124. if (listView.SelectedItems == null || listView.SelectedItems.Count == 0)
  125. {
  126. var item = (sender as MenuItem).DataContext as OpenFileInfo;
  127. if (item != null)
  128. {
  129. ViewModel?.RemoveFileItemCommand.Execute(item);
  130. }
  131. }
  132. else
  133. {
  134. ViewModel?.RemoveFilesFromContainerCommand.Execute(listView.SelectedItems);
  135. }
  136. }
  137. /// <summary>
  138. /// 右键菜单:打开文件路径
  139. /// </summary>
  140. /// <param name="sender"></param>
  141. /// <param name="e"></param>
  142. private void FilePathMenuItem_Click(object sender, RoutedEventArgs e)
  143. {
  144. if ((sender as MenuItem) != null)
  145. {
  146. var menuItem = sender as MenuItem;
  147. var file = menuItem.DataContext as OpenFileInfo;
  148. ViewModel?.ExplorerFileCommand.Execute(file);
  149. }
  150. }
  151. /// <summary>
  152. /// 双击打开最近列表
  153. /// </summary>
  154. private void RecentFilesList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  155. {
  156. var item = sender as ListViewItem;
  157. if (item != null && (item.DataContext as OpenFileInfo) != null)
  158. {
  159. var fileInfo = item.DataContext as OpenFileInfo;
  160. if(fileInfo != null)
  161. {
  162. ViewModel?.OpenRecentFilesCommand.Execute(fileInfo);
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 文件列表项:打开文件
  168. /// </summary>
  169. private void DocItemListViewControl_OpenFileItemHandler(object sender, RoutedEventArgs e)
  170. {
  171. var item = (sender as MenuItem).DataContext as OpenFileInfo;
  172. if (item != null)
  173. {
  174. ViewModel?.OpenRecentFilesCommand.Execute(item);
  175. }
  176. }
  177. /// <summary>
  178. /// 文件列表项:删除文件记录
  179. /// </summary>
  180. private void DocItemListViewControl_DeleteItemHandler(object sender, RoutedEventArgs e)
  181. {
  182. var item = (sender as MenuItem).DataContext as OpenFileInfo;
  183. if (item != null)
  184. {
  185. ViewModel?.RemoveFileItemCommand.Execute(item);
  186. }
  187. }
  188. #endregion
  189. private void UserControl_KeyDown(object sender, KeyEventArgs e)
  190. {
  191. //点击ESC取消选中
  192. if(GridRecentFilesList.Visibility == Visibility.Visible)
  193. {
  194. GridRecentFilesList.SelectedIndex = -1;
  195. }
  196. else
  197. {
  198. RecentFilesList.SelectedIndex = -1;
  199. }
  200. }
  201. }
  202. }