123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using PDF_Master.CustomControl;
- using PDF_Master.Helper;
- using PDF_Master.Properties;
- using PDF_Master.ViewModels.HomePanel.RecentFiles;
- using PDFSettings;
- 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_Master.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;
- }
- }
- //临时绑定文案
- (contexMenu.Items[0] as MenuItem).Header = ViewModel.T_MenuShow;
- (contexMenu.Items[1] as MenuItem).Header = ViewModel.T_MenuOpenFile;
- (contexMenu.Items[2] as MenuItem).Header = ViewModel.T_MenuRemove;
- if (listView.SelectedItems.Count >= 2)
- {
- foreach (var item in contexMenu.Items)
- {
- var menuitem = item as MenuItem;
- if (menuitem.Name == "FilePathMenuItem")
- menuitem.Visibility = Visibility.Collapsed;
- if (menuitem.Name == "MenuOpenFile")
- {
- menuitem.Visibility = Visibility.Visible;
- }
- }
- }
- else
- {
- foreach (var item in contexMenu.Items)
- {
- var menuitem = item as MenuItem;
- if (menuitem.Name == "MenuOpenFile")
- {
- menuitem.Visibility = Visibility.Collapsed;
- }
- }
- }
- }
- catch { }
- }
- /// <summary>
- /// 禁用触发鼠标右键,列表项被选中
- /// </summary>
- private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- Trace.WriteLine("Preview MouseRightButtonDown");
- e.Handled = true;
- }
- #endregion UI操作
- #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 Events
- private void UserControl_KeyDown(object sender, KeyEventArgs e)
- {
- //Windows:最近文件列表补充点击空白处取消选中;移除Esc取消选中逻辑
- //if (e.Key == Key.Escape)
- //{
- //点击ESC取消选中
- //if (GridRecentFilesList.Visibility == Visibility.Visible)
- //{
- // GridRecentFilesList.SelectedIndex = -1;
- //}
- //else
- //{
- // RecentFilesList.SelectedIndex = -1;
- //}
- //}
- }
- private void MenuOpenFile_Click(object sender, RoutedEventArgs e)
- {
- ListView listView;
- if (BtnListMode.IsChecked == true)
- listView = RecentFilesList;
- else
- listView = GridRecentFilesList;
- if (listView.SelectedItems.Count >= 2)
- {
- List<string> files = new List<string>();
- foreach (OpenFileInfo item in listView.SelectedItems)
- {
- var fileInfo = item;
- if (!File.Exists(fileInfo.FilePath))
- {
- SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
- }
- else
- {
- files.Add(fileInfo.FilePath);
- }
- }
- ViewModel?.LoadPdfViewer(files.ToArray());
- }
- }
- private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- //已经加载后,切换页签时,确认是否要刷新数据源
- if (IsLoaded && (bool)e.NewValue)
- {
- ViewModel.UpdateItemSoucre();
- }
- }
- }
- }
|