123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- using ComPDFKit.PDFDocument;
- using PDF_Office.Helper;
- using PDF_Office.ViewModels.BOTA;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
- namespace PDF_Office.Views.BOTA
- {
- /// <summary>
- /// BookmarkContent.xaml 的交互逻辑
- /// </summary>
- public partial class BookmarkContent : UserControl
- {
- private BookmarkContentViewModel viewModel = null;
- /// <summary>
- /// 上一个ListBoxItem,为选中状态做准备
- /// </summary>
- private ListBoxItem histotyListBoxItem = null;
- private bool isAdd = false;
- public BookmarkContent()
- {
- InitializeComponent();
- viewModel = this.DataContext as BookmarkContentViewModel;
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- KeyEventsHelper.KeyDown += ShortCut_KeyDown;
- }
- private void ShortCut_KeyDown(object sender, KeyEventArgs e)
- {
- if (KeyEventsHelper.IsSingleKey(Key.Escape))
- {
- if (BookMarkListView.SelectedItems == null)
- {
- return;
- }
- BookMarkListView.SelectedItems.Clear();
- }
- }
- /// <summary>
- ///ListViewItem,鼠标左键点击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListViewItem_MouseButtonDown(object sender, MouseButtonEventArgs e)
- {
- object[] objects = new object[] { sender, e };
- TextBlock textBlock = null;
- ListViewItem listBoxItem = (sender as ListViewItem);
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- if (e.ClickCount >= 2)
- {
- histotyListBoxItem = listBoxItem;
- if (e.OriginalSource is TextBlock)
- {
- textBlock = (TextBlock)e.OriginalSource;
- if (textBlock != null)
- {
- TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
- SetSelectedStatus(listBoxItem, textBox, textBlock);
- }
- }
- }
- else if (e.ClickCount == 1)
- {
- if (histotyListBoxItem != listBoxItem)
- {
- if (histotyListBoxItem != null)
- {
- histotyListBoxItem.IsSelected = false;
- textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
- textBlock.Visibility = Visibility.Visible;
- textBlock.Focusable = true;
- }
- }
- viewModel.ListViewItemMouseDownCommand.Execute(sender);
- }
- }
- else if (e.RightButton == MouseButtonState.Pressed)
- {
- ContextMenu contextMenu = listBoxItem.ContextMenu;
- if (contextMenu.Items.Count == 3)
- {
- MenuItem rename = contextMenu.Items[0] as MenuItem;
- MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
- MenuItem del = contextMenu.Items[2] as MenuItem;
- if (isSelects)
- {
- rename.IsEnabled = false;
- editPageIndex.IsEnabled = false;
- isSelects = false;
- }
- else
- {
- CPDFBookmark bookmark = (CPDFBookmark)listBoxItem.DataContext;
- if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
- {
- editPageIndex.IsEnabled = false;
- return;
- }
- rename.IsEnabled = true;
- editPageIndex.IsEnabled = true;
- editPageIndex.CommandParameter = listBoxItem.DataContext;
- editPageIndex.Command = viewModel.EditPageIndexCommand;
- }
- }
- }
- }
- private bool isSelects = false;
- private void BookMarkListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var a1 = (sender as ListView).SelectedItems.Count;
- if (a1 > 1)
- {
- isSelects = true;
- }
- }
- /// <summary>
- /// ListViewItem双击时选中状态
- /// </summary>
- /// <param name="listBoxItem"></param>
- /// <param name="textBox"></param>
- /// <param name="textBlock"></param>
- private void SetSelectedStatus(ListBoxItem listBoxItem, TextBox textBox, TextBlock textBlock)
- {
- listBoxItem.IsSelected = true;
- listBoxItem.Focus();
- textBlock.Visibility = Visibility.Collapsed;
- textBox.Dispatcher.BeginInvoke(new Action(() =>
- {
- textBox.Focus();
- textBox.SelectAll();
- //listBoxItem.IsSelected = true;
- }));
- }
- /// <summary>
- /// ListViewItem失去焦点
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
- {
- ListViewItem listItem = sender as ListViewItem;
- if (listItem != null)
- {
- BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
- viewModel.LostFocusCommand.Execute(listItem);
- TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
- TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
- if (textBlock.Visibility != Visibility.Visible)
- {
- if (isAdd == false && isRename == false)
- {
- listItem.IsSelected = false;
- }
- else
- {
- listItem.IsSelected = true;
- isAdd = false;
- }
- if (text.IsSelectionActive == true)
- {
- textBlock.Visibility = Visibility.Visible;
- }
- }
- else
- {
- listItem.IsSelected = true;
- }
- //if (histotyListBoxItem != null)
- //{
- // histotyListBoxItem.IsSelected = false;
- // TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
- // textBlock.Visibility = Visibility.Visible;
- // textBlock.Focusable = true;
- //}
- //if (textBlock.Visibility == Visibility.Collapsed)
- //{
- // listItem.IsSelected = false;
- // //textBlock.Visibility = Visibility.Visible;
- //}
- //else
- //{
- // listItem.IsSelected = true;
- // //textBlock.Visibility = Visibility.Collapsed;
- //}
- }
- }
- private bool isRename = false;
- /// <summary>
- /// 右键菜单-重命名
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MenuItemRename_Click(object sender, RoutedEventArgs e)
- {
- if (sender is MenuItem)
- {
- MenuItem menuItem = (MenuItem)sender;
- CPDFBookmark bookmark = menuItem.CommandParameter as CPDFBookmark;
- if (bookmark != null)
- {
- BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
- isRename = true;
- ListViewItem listBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
- histotyListBoxItem = listBoxItem;
- TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
- TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
- SetSelectedStatus(listBoxItem, textBox, textBlock);
- }
- }
- }
- /// <summary>
- /// BookMarkListView,鼠标点击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- var pos = e.GetPosition(BookMarkListView);
- var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
- if (result != null)
- {
- //获取当前鼠标指针下的容器
- var listBoxItem = CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
- if (listBoxItem == null)
- {
- if (BookMarkListView.SelectedItem != null)
- {
- ListViewItem item = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
- item.IsSelected = false;
- TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
- box.Visibility = Visibility.Visible;
- BookMarkListView.SelectedItems.Clear();
- }
- if (histotyListBoxItem != null)
- {
- var pos1 = e.GetPosition(histotyListBoxItem);
- var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
- if (result1 == null)
- {
- histotyListBoxItem.IsSelected = false;
- TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
- textBlock.Visibility = Visibility.Visible;
- textBlock.Focusable = true;
- }
- }
- }
- else
- {
- TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
- TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
- if (textBox.IsFocused == false)
- {
- listBoxItem.Focus();
- }
- }
- }
- //BookMarkListView.Focus();
- }
- }
- /// <summary>
- /// 右键菜单-删除
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
- {
- List<int> pagelist = new List<int>();
- for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
- {
- CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
- pagelist.Add(BookMarkListView.Items.IndexOf(item));
- }
- pagelist.Sort();
- for (int i = 0; i < pagelist.Count; i++)
- {
- CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
- if (data != null)
- {
- viewModel.DeleteCommand.Execute(data);
- }
- }
- }
- public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
- {
- isAdd = true;
- viewModel.AddBookmarkCommand.Execute(BookMarkListView);
- if (viewModel.IsHasBookmark == false)
- {
- BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
- ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
- if (myListBoxItem == null)
- {
- BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
- BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
- }
- else
- {
- TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
- TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
- SetSelectedStatus(myListBoxItem, textBox, textBlock);
- }
- }
- }
- private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
- {
- ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
- if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
- {
- BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
- ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
- if (myListBoxItem != null)
- {
- if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
- {
- BookMarkListView.UpdateLayout();
- myListBoxItem.UpdateLayout();
- BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
- }
- TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
- TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
- SetSelectedStatus(myListBoxItem, textBox, textBlock);
- }
- }
- }
- private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
- {
- if (BookMarkListView.SelectedItems == null)
- {
- return;
- }
- if (e.Key == Key.Escape)
- {
- BookMarkListView.SelectedItems.Clear();
- }
- //if (e.Key == Key.Delete)
- //{
- // if (BookMarkListView.SelectedItems.Count > 0)
- // {
- // //全选删除
- // if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
- // {
- // }
- // }
- //}
- }
- }
- }
|