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 { }
}
///
/// 禁用触发鼠标右键,列表项被选中
///
private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
Trace.WriteLine("Preview MouseRightButtonDown");
e.Handled = true;
}
#endregion
#region Events
///
/// 右键菜单:删除文件记录
///
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);
}
}
///
/// 右键菜单:打开文件路径
///
///
///
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);
}
}
///
/// 双击打开最近列表
///
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);
}
}
}
///
/// 文件列表项:打开文件
///
private void DocItemListViewControl_OpenFileItemHandler(object sender, RoutedEventArgs e)
{
var item = (sender as MenuItem).DataContext as OpenFileInfo;
if (item != null)
{
ViewModel?.OpenRecentFilesCommand.Execute(item);
}
}
///
/// 文件列表项:删除文件记录
///
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;
}
}
}
}