123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- 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.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Windows.Media;
- using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum;
- using static System.Net.Mime.MediaTypeNames;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- 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<CPDFBookmark> bookmarklist;
- public ObservableCollection<CPDFBookmark> 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<object> MouseClickCommand { get; set; }
- public DelegateCommand<object> MouseEnterCommand { get; set; }
- public DelegateCommand<object> MouseLeaveCommand { get; set; }
- public DelegateCommand<object> LostFocusCommand { get; set; }
- public DelegateCommand<object> GotFocusCommand { get; set; }
- public DelegateCommand<object> AddBookmarkCommand { get; set; }
- public DelegateCommand<object> ListViewItemMouseDownCommand { get; set; }
- public DelegateCommand<object> RenameCommand { get; set; }
- public DelegateCommand<object> EditPageIndexCommand { get; set; }
- public DelegateCommand<object> DeleteCommand { get; set; }
- #endregion 命令
- public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService)
- {
- region = regionManager;
- dialogs = dialogService;
- MouseClickCommand = new DelegateCommand<object>(MouseClickEvent);
- MouseEnterCommand = new DelegateCommand<object>(MouseEnterEvent);
- LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
- MouseLeaveCommand = new DelegateCommand<object>(MouseLeaveEvent);
- GotFocusCommand = new DelegateCommand<object>(GotFocusEvent);
- AddBookmarkCommand = new DelegateCommand<object>(AddBookmarkEvent);
- ListViewItemMouseDownCommand = new DelegateCommand<object>(ListViewItemMouseLeftButtonDownEvent);
- RenameCommand = new DelegateCommand<object>(RenameEvent);
- DeleteCommand = new DelegateCommand<object>(DelegateEvent);
- EditPageIndexCommand = new DelegateCommand<object>(EditPageIndexEvent);
- }
- private void EditPageIndexEvent(object obj)
- {
- throw new NotImplementedException();
- }
- private void DelegateEvent(object obj)
- {
- }
- private void RenameEvent(object obj)
- {
- CPDFBookmark bookmark = obj as CPDFBookmark;
- if (bookmark != null)
- {
- //ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
- }
- }
- private void GotFocusEvent(object obj)
- {
- if (obj is CompositeCommandParameter)
- {
- CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
- if (parameter.Parameter is TextBox)
- {
- TextBox textBox = (TextBox)parameter.Parameter;
- textBox.SelectAll();
- textBox.PreviewMouseDown -= new MouseButtonEventHandler(TxtTitleInput_PreviewMouseLeftButtonDown);
- }
- }
- }
- private void LostFocusEvent(object obj)
- {
- if (obj is CompositeCommandParameter)
- {
- CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
- if (parameter.Parameter is TextBox)
- {
- TextBox textBox = (TextBox)parameter.Parameter;
- //textBox.PreviewMouseDown += new MouseButtonEventHandler(TxtTitleInput_PreviewMouseLeftButtonDown);
- var item = CommonHelper.FindVisualParent<ListViewItem>(textBox);
- if (item != null)
- {
- var data = item.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)
- {
- object[] objs = obj as object[];
- if (objs != null && objs.Length > 0)
- {
- if (objs[1] is MouseButtonEventArgs)
- {
- var mouse = (MouseButtonEventArgs)objs[1];
- if (mouse.LeftButton == MouseButtonState.Pressed)
- {
- TextBlock box = null;
- StackPanel stackPanel = null;
- ListBoxItem listBoxItem = (objs[0] as ListBoxItem);
- if (mouse.ClickCount >= 2)
- {
- if (mouse.OriginalSource is TextBlock)
- {
- box = (TextBlock)mouse.OriginalSource;
- stackPanel = (box.Parent as Grid).Parent as StackPanel;
- if (box != null)
- {
- listBoxItem.IsSelected = true;
- listBoxItem.Focus();
- box.Visibility = Visibility.Collapsed;
- TextBox textBox = CommonHelper.FindVisualChild<TextBox>(stackPanel);
- textBox.Dispatcher.BeginInvoke(new Action(() =>
- {
- textBox.Focus();
- textBox.SelectAll();
- }));
- //textBox.PreviewMouseDown += new MouseButtonEventHandler(TxtTitleInput_PreviewMouseLeftButtonDown);
- //TxtTitleInput_PreviewMouseLeftButtonDown(textBox, mouse);
- isSelete = true;
- }
- }
- }
- if (mouse.ClickCount == 1)
- {
- if (listBoxItem != null)
- {
- box = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
- //box.Background = listBoxItem.Background;
- //box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
- int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
- PDFViewer.GoToPage(index);
- isSelete = false;
- }
- }
- //MessageBox.Show("dsafa");
- }
- }
- }
- }
- private void MouseLeaveEvent(object obj)
- {
- if (obj is CompositeCommandParameter)
- {
- CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
- if (parameter.Parameter is Grid)
- {
- Grid grid = parameter.Parameter as Grid;
- if (grid != null)
- {
- TextBlock box = CommonHelper.FindVisualChild<TextBlock>(grid);
- ListBoxItem myListBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(grid);
- box.Visibility = Visibility.Visible;
- //if (myListBoxItem.IsSelected == true)
- //{
- // box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
- //}
- //if (myListBoxItem.IsMouseOver == true)
- //{
- // box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
- //}
- //else
- //{
- // box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
- //}
- //myListBoxItem.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
- //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(defaultColor));
- }
- }
- }
- }
- public void MouseEnterEvent(object obj)
- {
- if (obj is CompositeCommandParameter)
- {
- CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
- if (parameter.Parameter is Grid)
- {
- Grid grid = parameter.Parameter as Grid;
- if (grid != null)
- {
- TextBlock box = CommonHelper.FindVisualChild<TextBlock>(grid);
- ListBoxItem listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(grid);
- if (box != null)
- {
- box.Visibility = Visibility.Visible;
- //box.Background = listBoxItem.Background;
- //box.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
- }
- //myListBoxItem.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
- //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hoverColor));
- }
- }
- }
- }
- 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<ContentPresenter>(myListBoxItem);
- //DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
- //Grid item = (Grid)myDataTemplate.FindName("Grid", myContentPresenter);
- //Grid grid = CommonHelper.FindVisualChild<Grid>(myListBoxItem);
- 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<string>(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<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.Title));
- }
- }
- }
- });
- }
- public void MouseClickEvent(object obj)
- {
- if (obj is MouseButtonEventArgs)
- {
- var mouse = (MouseButtonEventArgs)obj;
- if (mouse.LeftButton == MouseButtonState.Pressed)
- {
- TextBlock box = null;
- Grid grid = null;
- if (mouse.ClickCount >= 2)
- {
- if (mouse.Source is Grid)
- {
- grid = mouse.Source as Grid;
- //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
- if (grid != null)
- {
- box = CommonHelper.FindVisualChild<TextBlock>(grid);
- //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
- if (box != null)
- {
- box.Visibility = Visibility.Collapsed;
- //TextBox textBox = CommonHelper.FindVisualChild<TextBox>((box.Parent as StackPanel));
- //FocusManager.SetFocusedElement(box.Parent, textBox);
- //Keyboard.Focus(textBox);
- //textBox.IsHitTestVisible = true;
- //textBox.Focus();
- }
- }
- }
- if (mouse.Source is TextBlock)
- {
- box = (TextBlock)mouse.Source;
- grid = (box.Parent as StackPanel).Parent as Grid;
- //grid.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(selectColcr));
- if (box != null)
- {
- box.Visibility = Visibility.Collapsed;
- //TextBox textBox = CommonHelper.FindVisualChild<TextBox>((box.Parent as StackPanel));
- //FocusManager.SetFocusedElement(box.Parent, textBox);
- //Keyboard.Focus(textBox);
- //textBox.IsHitTestVisible = true;
- //textBox.Focus();
- }
- }
- }
- //MessageBox.Show("dsafa");
- }
- }
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- if (PDFViewer == null)
- {
- return;
- }
- Bookmarklist = new ObservableCollection<CPDFBookmark>(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)
- {
- }
- }
- }
|