123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using PDF_Office.CustomControl;
- using PDF_Office.Helper;
- using PDF_Office.Properties;
- using PDF_Office.ViewModels.HomePanel.RecentFiles;
- using PDFSettings.Settings;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.IO;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Interop;
- using System.Windows.Navigation;
- using winform = System.Windows.Forms;
- namespace PDF_Office.Views.HomePanel.RecentFiles
- {
- public partial class RecentFilesContent : UserControl
- {
- private RecentFilesContentViewModel ViewModel => DataContext as RecentFilesContentViewModel;
- public RecentFilesContent()
- {
- InitializeComponent();
- }
- #region 初始化
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- InitVariables();
- InitEvents();
- }
- private void InitVariables()
- {
- if (RecentFilesList.SelectedItems != null)
- RecentFilesList.SelectedItems.Clear();
- if (GridRecentFilesList.SelectedItems != null)
- GridRecentFilesList.SelectedItems.Clear();
- }
- private void InitEvents()
- {
- ViewModel.RecentFilesSelectionHandler -= ViewModel_RecentFilesSelection;
- ViewModel.RecentFilesSelectionHandler += ViewModel_RecentFilesSelection;
- }
- #endregion
- #region UI操作
- private void ViewModel_RecentFilesSelection(object sender, bool e)
- {
- if (e)
- {
- RecentFilesList.SelectedItems.Clear();
- if (GridRecentFilesList.SelectedItems.Count > 0)
- {
- for (int i = 0; i < GridRecentFilesList.SelectedItems.Count; i++)
- {
- RecentFilesList.SelectedItems.Add(GridRecentFilesList.SelectedItems[i]);
- }
- }
- }
- else
- {
- GridRecentFilesList.SelectedItems.Clear();
- if (RecentFilesList.SelectedItems.Count > 0)
- {
- for (int i = 0; i < RecentFilesList.SelectedItems.Count; i++)
- {
- GridRecentFilesList.SelectedItems.Add(RecentFilesList.SelectedItems[i]);
- }
- }
- }
- }
- private void ContextMenu_Opened(object sender, RoutedEventArgs e)
- {
- try
- {
- ListView listView;
- if (BtnListMode.IsChecked == true)
- listView = RecentFilesList;
- else
- listView = GridRecentFilesList;
- var contexMenu = sender as ContextMenu;
- if (contexMenu != null)
- {
- foreach (var item in contexMenu.Items)
- {
- var menuitem = item as MenuItem;
- menuitem.Visibility = Visibility.Visible;
- }
- }
- if (listView.SelectedItems.Count >= 2)
- {
- foreach (var item in contexMenu.Items)
- {
- var menuitem = item as MenuItem;
- if (menuitem.Name == "FilePathMenuItem")
- menuitem.Visibility = Visibility.Collapsed;
- }
- }
- }
- catch { }
- }
- /// <summary>
- /// 禁用触发鼠标右键,列表项被选中
- /// </summary>
- private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- Trace.WriteLine("Preview MouseRightButtonDown");
- e.Handled = true;
- }
- #endregion
- #region Events
- /// <summary>
- /// 右键菜单:删除文件记录
- /// </summary>
- private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
- {
- ListView listView;
- if (BtnListMode.IsChecked == true)
- listView = RecentFilesList;
- else
- listView = GridRecentFilesList;
- if (listView.SelectedItems == null || listView.SelectedItems.Count == 0)
- {
- var item = (sender as MenuItem).DataContext as OpenFileInfo;
- if (item != null)
- {
- ViewModel?.RemoveFileItemCommand.Execute(item);
- }
- }
- else
- {
- ViewModel?.RemoveFilesFromContainerCommand.Execute(listView.SelectedItems);
- }
- }
- /// <summary>
- /// 右键菜单:打开文件路径
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void FilePathMenuItem_Click(object sender, RoutedEventArgs e)
- {
- if ((sender as MenuItem) != null)
- {
- var menuItem = sender as MenuItem;
- var file = menuItem.DataContext as OpenFileInfo;
- ViewModel?.ExplorerFileCommand.Execute(file);
- }
- }
- /// <summary>
- /// 双击打开最近列表
- /// </summary>
- private void RecentFilesList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- var item = sender as ListViewItem;
- if (item != null && (item.DataContext as OpenFileInfo) != null)
- {
- var fileInfo = item.DataContext as OpenFileInfo;
- if(fileInfo != null)
- {
- ViewModel?.OpenRecentFilesCommand.Execute(fileInfo);
- }
- }
- }
- /// <summary>
- /// 文件列表项:打开文件
- /// </summary>
- private void DocItemListViewControl_OpenFileItemHandler(object sender, RoutedEventArgs e)
- {
- var item = (sender as MenuItem).DataContext as OpenFileInfo;
- if (item != null)
- {
- ViewModel?.OpenRecentFilesCommand.Execute(item);
- }
- }
- /// <summary>
- /// 文件列表项:删除文件记录
- /// </summary>
- private void DocItemListViewControl_DeleteItemHandler(object sender, RoutedEventArgs e)
- {
- var item = (sender as MenuItem).DataContext as OpenFileInfo;
- if (item != null)
- {
- ViewModel?.RemoveFileItemCommand.Execute(item);
- }
- }
- #endregion
- private void UserControl_KeyDown(object sender, KeyEventArgs e)
- {
- //点击ESC取消选中
- if(GridRecentFilesList.Visibility == Visibility.Visible)
- {
- GridRecentFilesList.SelectedIndex = -1;
- }
- else
- {
- RecentFilesList.SelectedIndex = -1;
- }
- }
- }
- }
|