using ComPDFKit.PDFDocument; using ComPDFKitViewer.PdfViewer; using ImTools; using Microsoft.Office.Interop.Word; using PDF_Office.CustomControl; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.Model.PageEdit; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum; using static System.Net.Mime.MediaTypeNames; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using KeyEventArgs = System.Windows.Input.KeyEventArgs; using ListViewItem = System.Windows.Controls.ListViewItem; using TextBox = System.Windows.Controls.TextBox; namespace PDF_Office.ViewModels.BOTA { public class BookmarkContentViewModel : BindableBase, INavigationAware { #region 属性 private static string hoverColor = "#D9D9D9"; private static string defaultColor = "#F2F2F2"; private static string selectColcr = "#97D7FB"; private static string borderBackground = "#FFFFFF"; private static string borderBrush = "#000000"; private bool isSelete = false; private IRegionManager region; private IDialogService dialogs; public CPDFViewer PDFViewer; private ObservableCollection bookmarklist; public ObservableCollection Bookmarklist { get { return bookmarklist; } set { SetProperty(ref bookmarklist, value); } } private Visibility isEmpty; public Visibility IsEmptyPanelVisibility { get { return isEmpty; } set { SetProperty(ref isEmpty, value); } } #endregion 属性 #region 命令 public DelegateCommand KeyDownCommand { get; set; } public DelegateCommand LostFocusCommand { get; set; } public DelegateCommand AddBookmarkCommand { get; set; } public DelegateCommand ListViewItemMouseDownCommand { get; set; } public DelegateCommand RenameCommand { get; set; } public DelegateCommand EditPageIndexCommand { get; set; } public DelegateCommand DeleteCommand { get; set; } #endregion 命令 public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService) { region = regionManager; dialogs = dialogService; //MouseClickCommand = new DelegateCommand(MouseClickEvent); //MouseEnterCommand = new DelegateCommand(MouseEnterEvent); LostFocusCommand = new DelegateCommand(LostFocusEvent); //MouseLeaveCommand = new DelegateCommand(MouseLeaveEvent); //GotFocusCommand = new DelegateCommand(GotFocusEvent); AddBookmarkCommand = new DelegateCommand(AddBookmarkEvent); ListViewItemMouseDownCommand = new DelegateCommand(ListViewItemMouseLeftButtonDownEvent); RenameCommand = new DelegateCommand(RenameEvent); DeleteCommand = new DelegateCommand(DelegateEvent); EditPageIndexCommand = new DelegateCommand(EditPageIndexEvent); KeyDownCommand = new DelegateCommand(KeyDownEvent); } private void KeyDownEvent(object obj) { if (obj is KeyEventArgs) { KeyEventArgs keyEventArgs = (KeyEventArgs)obj; if (keyEventArgs.Key == Key.Enter) { if (keyEventArgs.OriginalSource is TextBox) { TextBox textBox = (TextBox)keyEventArgs.OriginalSource; ListViewItem listViewItem = CommonHelper.FindVisualParent(textBox); UpdateTitle(listViewItem, textBox); TextBlock textBlock = CommonHelper.FindVisualChild(listViewItem); textBlock.Text = textBox.Text; textBlock.Visibility = Visibility.Visible; } } } } private void EditPageIndexEvent(object obj) { CPDFBookmark bookmark = obj as CPDFBookmark; if (bookmark != null) { //var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == bookmark.PageIndex && q.Title == bookmark.Title); //ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0])); //if (myListBoxItem.IsSelected == false) //{ // myListBoxItem.IsSelected = true; // myListBoxItem.Focus(); //} DialogParameters value = new DialogParameters(); value.Add(ParameterNames.BookmarkInfo, "确定要将选定大纲的目标位置设置为当前位置吗?"); dialogs.ShowDialog(DialogNames.BookmarkInfoDialog, value, e => { if (e.Result == ButtonResult.OK) { if (RemoveBookMark(bookmark)) { bookmarklist.Remove(bookmark); CPDFBookmark cPDFBookmark = new CPDFBookmark(); bookmark.Title = bookmark.Title; bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n"; bookmark.PageIndex = PDFViewer.CurrentIndex; if (PDFViewer.Document.AddBookmark(bookmark)) { PDFViewer.UndoManager.CanSave = true; Bookmarklist = new ObservableCollection(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title)); } } } }); } } private void DelegateEvent(object obj) { CPDFBookmark bookmark = obj as CPDFBookmark; if (bookmark != null) { if (RemoveBookMark(bookmark)) bookmarklist.Remove(bookmark); //ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem)); } } private void RenameEvent(object obj) { CPDFBookmark bookmark = obj as CPDFBookmark; if (bookmark != null) { //var result = PDFViewer.Document.EditBookmark(bookmark.PageIndex, textBox.Text.Trim()); //if (result) //{ // bookmark.Title = textBox.Text.Trim(); // PDFViewer.UndoManager.CanSave = true; //} } } private bool RemoveBookMark(CPDFBookmark item) { var data = item; if (data == null) return false; return PDFViewer.Document.RemoveBookmark(data.PageIndex); } private void LostFocusEvent(object obj) { TextBox textBox = null; ListViewItem listViewItem = null; if (obj is CompositeCommandParameter) { CompositeCommandParameter parameter = (CompositeCommandParameter)obj; if (parameter.Parameter is TextBox) { textBox = (TextBox)parameter.Parameter; //textBox.PreviewMouseDown += new MouseButtonEventHandler(TxtTitleInput_PreviewMouseLeftButtonDown); listViewItem = CommonHelper.FindVisualParent(textBox); } } if (obj is ListBoxItem) { listViewItem = (ListViewItem)(obj); textBox = CommonHelper.FindVisualChild(listViewItem); } UpdateTitle(listViewItem, textBox); } private void UpdateTitle(ListViewItem listViewItem, TextBox textBox) { if (listViewItem != null) { var data = listViewItem.DataContext as CPDFBookmark; if (data == null) return; var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim()); if (result) { data.Title = textBox.Text.Trim(); PDFViewer.UndoManager.CanSave = true; } } } private void TxtTitleInput_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { TextBox textBox = sender as TextBox; textBox.Focus(); e.Handled = true; } private void ListViewItemMouseLeftButtonDownEvent(object obj) { ListBoxItem listBoxItem = (obj as ListBoxItem); if (listBoxItem != null) { //box.Background = listBoxItem.Background; //box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr)); if (!(listBoxItem.DataContext is CPDFBookmark)) { return; } int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex; PDFViewer.GoToPage(index); isSelete = false; } //MessageBox.Show("dsafa"); } private void AddBookmarkEvent(object obj) { int index = PDFViewer.CurrentIndex; string mark = string.Format($"第{index + 1}页"); var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index); if (list.Count > 0) { System.Windows.Controls.ListView listView = obj as System.Windows.Controls.ListView; if (listView != null) { //listView.Items.CurrentItem = list[0]; ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0])); //ContentPresenter myContentPresenter = CommonHelper.FindVisualChild(myListBoxItem); //DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; //Grid item = (Grid)myDataTemplate.FindName("Grid", myContentPresenter); //Grid grid = CommonHelper.FindVisualChild(myListBoxItem); if (myListBoxItem.IsSelected == false) { myListBoxItem.IsSelected = true; myListBoxItem.Focus(); } //listView.SelectedItem = myListBoxItem; //myListBoxItem.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr)); return; } } DialogParameters value = new DialogParameters(); value.Add(ParameterNames.Bookmark, mark); value.Add(ParameterNames.Title, "创建一个新的书签"); dialogs.ShowDialog(DialogNames.AddBookmarkDialog, value, e => { if (e.Result == ButtonResult.OK && e.Parameters != null) { if (e.Parameters.ContainsKey(ParameterNames.Bookmark)) { mark = e.Parameters.GetValue(ParameterNames.Bookmark).ToString(); CPDFBookmark bookmark = new CPDFBookmark(); bookmark.Title = mark; bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n"; bookmark.PageIndex = PDFViewer.CurrentIndex; if (PDFViewer.Document.AddBookmark(bookmark)) { PDFViewer.UndoManager.CanSave = true; Bookmarklist = new ObservableCollection(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title)); } } } }); } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer == null) { return; } Bookmarklist = new ObservableCollection(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title)); if (Bookmarklist.Count < 1) { IsEmptyPanelVisibility = Visibility.Visible; return; } else { IsEmptyPanelVisibility = Visibility.Hidden; return; } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } } }