123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872 |
- using PDF_Office.EventAggregators;
- using PDF_Office.Helper;
- using PDF_Office.Model.PageEdit;
- using PDF_Office.ViewModels.PageEdit;
- using Prism.Events;
- 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.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- using WpfToolkit.Controls;
- namespace PDF_Office.Views.PageEdit
- {
- /// <summary>
- /// PageEditContent.xaml 的交互逻辑
- /// </summary>
- public partial class PageEditContent : UserControl
- {
- /// <summary>
- /// 用于通知刷新图片的事件
- /// 因为目前的图片刷新策略依赖很多UI事件判断,因为将主要判断逻辑放在UI层,VM负责图片刷新,非必要情况尽量不要采用这种形式
- /// </summary>
- private IEventAggregator eventor;
- /// <summary>
- /// 暂存的唯一索引值,用于区分多页签
- /// </summary>
- private string unicode;
- /// <summary>
- /// 是否是加载时第一次触发滚动
- /// </summary>
- private bool isFirstScrollChange = true;
- /// <summary>
- /// 用于判断滚轮停止的计时器
- /// </summary>
- private DispatcherTimer timer = new DispatcherTimer();
- /// <summary>
- /// 判断是否开始框选
- /// </summary>
- private bool startChoose = false;
- /// <summary>
- /// 框选的起始位置
- /// </summary>
- private Point starPosition = new Point();
- /// <summary>
- /// 记录当前滑块的状态
- /// </summary>
- private ScrollEventType scrolltype = ScrollEventType.EndScroll;
- //鼠标点击时在item中的位置 实现类似点哪拖哪的细节
- private double item_x;
- private double item_y;
- //插入标记代表的插入位置
- private int InsertIndex = -1;
- //拖动的Item
- private PageEditItem tempItem;
- /// <summary>
- /// 是否正在拖拽排序中,通过该变量避免单击触发拖动
- /// </summary>
- private bool isDraging = false;
- //是否正在从外部拖入文件
- public bool isDragingEnter { get; set; } = false;
- /// <summary>
- /// 是否需要自动滚动
- /// </summary>
- private bool needScroll = false;
- //自动滚动速度
- private int speed = 0;
- private PageEditContentViewModel viewModel;
- public PageEditContent()
- {
- InitializeComponent();
- }
- public PageEditContent(IEventAggregator eventAggregator) : this()
- {
- eventor = eventAggregator;
- unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- timer.Interval = TimeSpan.FromSeconds(0.3);
- timer.Tick += Timer_Tick;
- viewModel = this.DataContext as PageEditContentViewModel;
- //订阅页面刷新事件
- eventor.GetEvent<PageEditNotifyEvent>().Subscribe(OneNotifyEvent, e => e.Unicode == unicode);
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- PulishEvent();
- timer.Stop();
- }
- private void OneNotifyEvent(PageEditNotifyEventArgs e)
- {
- if (e.Type == NotifyType.RefreshPage)
- {
- PulishEvent();
- }
- else
- {
- if (e.Type == NotifyType.SelectItems)
- {
- if (e.PageRange.Count == 1)
- {
- ListPageEdit.SelectedIndex = e.PageRange[0] - 1;
- ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
- }
- else
- {
- ListPageEdit.SelectedItems.Clear();
- for (int i = 0; i < e.PageRange.Count; i++)
- {
- ListPageEdit.SelectedItems.Add(ListPageEdit.Items[e.PageRange[i] - 1]);
- }
- }
- }
- }
- }
- #region UI事件
- /// <summary>
- /// 每次显示的时候就触发事件,刷新所有图片
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- if ((bool)e.NewValue)
- {
- //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
- PulishEvent();
- //当前页面发生变化时通过ScrollChanged事件来刷新图片
- isFirstScrollChange = true;
- }
- }
- /// <summary>
- /// 鼠标滚轮滚动时触发的事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListPageEdit_ScrollChanged(object sender, ScrollChangedEventArgs e)
- {
- if (isFirstScrollChange)
- {
- PulishEvent();//第一次加载时触发的Scollchange 直接刷新界面,减少白板显示时间
- isFirstScrollChange = false;
- return;
- }
- else
- {
- if (e.VerticalChange != 0)
- timer.Start();//暂时找不到比较好的 判断Scroller停止的方法,先用计时器粗略判断
- }
- }
- /// <summary>
- /// 拖动右侧滑块时触发的事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListPageEdit_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
- {
- scrolltype = e.ScrollEventType;
- if (scrolltype != System.Windows.Controls.Primitives.ScrollEventType.EndScroll)
- {
- timer.Stop();
- return;
- }
- PulishEvent();
- }
- #endregion
- #region 方法
- /// <summary>
- /// 发布事件
- /// </summary>
- private void PulishEvent()
- {
- var itemSize = (ListPageEdit.Items[0] as PageEditItem).ItemSize;
- var range = GetRoughFromView(ListPageEdit, itemSize, new Thickness(5, 10, 5, 10));
- eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
- }
- /// <summary>
- /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
- /// 返回值为页面范围 从1开始
- /// </summary>
- /// <param name="view"></param>
- /// <param name="itemSize"></param>
- /// <param name="itemMargin"></param>
- /// <returns></returns>
- private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
- {
- //var scrollViewer = GetScrollHost(view);
- var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
- if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
- return new Tuple<int, int, int>(0, 0, 0);
- try
- {
- var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
- var currentWidth = scrollViewer.ActualWidth;
- //计算当前窗口大小能显示的行数和列数
- var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
- var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
- var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
- preItemCount = preItemCount * columnCount;
- var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
- preEnd = preEnd * columnCount + columnCount - 1;
- var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
- return new Tuple<int, int, int>(
- Math.Max(preItemCount, 0),
- Math.Min(view.Items.Count, preEnd),
- middle);
- }
- catch { }
- return new Tuple<int, int, int>(0, 0, 0);
- }
- /// <summary>
- /// 获取listbox里的ScrollViewer对象
- /// 留意如果有重写ListBox对象,该方法可能无效
- /// </summary>
- /// <param name="listBox"></param>
- /// <returns></returns>
- private ScrollViewer GetScrollHost(ListBox listBox)
- {
- if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
- {
- int s = VisualTreeHelper.GetChildrenCount(listBox);
- Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
- if (border != null)
- {
- return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
- }
- }
- return null;
- }
- #endregion
- /// <summary>
- /// 输入框显示时,自动获取焦点
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- if ((bool)e.NewValue)
- {
- (sender as Control).Focus();
- }
- }
- private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- try
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- //鼠标框选逻辑
- if (startChoose)
- {
- var position = e.GetPosition(ListPageEdit);
- if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
- {
- startChoose = false;
- RectChoose.Visibility = Visibility.Collapsed;
- //暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
- Mouse.Capture(null);
- return;
- }
- //矩形框内的item设为选中
- DoSelectItems();
- return;
- }
- //拖拽排序的逻辑
- var pos = e.GetPosition(ListPageEdit);
- if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
- {
- LineInset.Visibility = Visibility.Collapsed;
- ImgPicture.Visibility = Visibility.Collapsed;
- return;
- }
- HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
- if (result == null)
- {
- return;
- }
- var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (listBoxItem == null)
- {
- return;
- }
- isDragingEnter = false;
- tempItem = listBoxItem.DataContext as PageEditItem;
- var item_pos = e.GetPosition(listBoxItem);
- if (item_pos != null)
- {
- item_x = item_pos.X;
- item_y = item_pos.Y;
- }
- var scroll = GetScrollHost(ListPageEdit);
- string filename = Guid.NewGuid().ToString() + ".pdf";
- string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
- System.IO.File.Create(tempPath);
- string[] files = new string[1];
- files[0] = tempPath;
- DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
- DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
- Mouse.Capture(ListPageEdit);
- return;
- }
- RectChoose.Visibility = Visibility.Collapsed;
- startChoose = false;
- Mouse.Capture(null);
- }
- catch
- {
- LineInset.Visibility = Visibility.Collapsed;
- ImgPicture.Visibility = Visibility.Collapsed;
- }
- }
- /// <summary>
- /// 判断是否开始框选、记录框选起始位置
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var pos = e.GetPosition(ListPageEdit);
- HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
- if (result == null)
- {
- return;
- }
- //未选中item 并且不是点击滑轨时 开始框选
- var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
- if (listBoxItem == null)
- {
- if (scroller != null)
- {
- startChoose = false;
- return;
- }
- //点击空白处时开始框选
- startChoose = true;
- if (ListPageEdit.SelectedItems.Count > 0)
- {
- ListPageEdit.SelectedItems.Clear();
- }
- starPosition = e.GetPosition(ListPageEdit);
- starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
- Mouse.Capture(ListPageEdit);
- return;
- }
- //选中了item 时,不能框选
- startChoose = false;
- //更改系统的选中规则,选中状态下,鼠标松开后取消选中
- //方便实现多选拖拽功能
- if (listBoxItem.IsSelected == true && !Keyboard.IsKeyDown(Key.LeftCtrl))
- {
- e.Handled = true;
- }
- }
- private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- var pos = e.GetPosition(ListPageEdit);
- HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
- if (result == null)
- {
- return;
- }
- var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (listBoxItem == null)
- {
- return;
- }
- //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
- //拖拽框选之后的抬起鼠标不进入处理
- if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
- {
- ListPageEdit.SelectedItems.Clear();
- ListPageEdit.SelectedItem = listBoxItem;
- listBoxItem.IsSelected = true;
- return;
- }
- Mouse.Capture(null);
- //结束鼠标框选
- startChoose = false;
- RectChoose.Visibility = Visibility.Collapsed;
- }
- /// <summary>
- /// 获取Listobox的Wrappanel容器
- /// </summary>
- /// <param name="listBox"></param>
- /// <returns></returns>
- public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
- {
- Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
- var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
- return panel;
- }
- /// <summary>
- ///根据鼠标拖选的框 选中矩形框里面的Item
- /// </summary>
- private void DoSelectItems()
- {
- var s = GetScrollHost(ListPageEdit);
- Point start = new Point();
- //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
- start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
- var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
- RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
- RectChoose.Width = rec.Width;
- RectChoose.Height = rec.Height;
- RectChoose.Visibility = Visibility.Visible;
- //检测遍历所有项,筛选在矩形框中的Item
- for (int i = 0; i < ListPageEdit.Items.Count; i++)
- {
- var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
- //通过这一步来避免重复误选中
- var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
- if (parent == null)
- continue;
- var v = VisualTreeHelper.GetOffset(_item);
- if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
- {
- ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
- }
- else
- {
- ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
- }
- }
- return;
- }
- /// <summary>
- /// 退出拖拽模式
- /// </summary>
- private void ExitDraging()
- {
- LineInset.Visibility = Visibility.Collapsed;
- ImgPicture.Visibility = Visibility.Collapsed;
- isDraging = false;
- }
- /// <summary>
- /// 拖拽释放后的处理逻辑
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private async void ListPageEdit_Drop(object sender, DragEventArgs e)
- {
- needScroll = false;
- if (!isDraging)
- {
- //未拖拽时隐藏插入标记和虚影
- LineInset.Visibility = Visibility.Collapsed;
- ImgPicture.Visibility = Visibility.Collapsed;
- return;
- }
- #region 功能付费锁
- // //if (!App.IsActive())
- // //{
- // // MidLane.Visibility = Visibility.Collapsed;
- // // IAPFunctionDialog dialog = new IAPFunctionDialog("PageEdit");
- // // dialog.ShowDialog();
- // // return;
- // //}
- #endregion
- #region 从外部拖拽插入文件
- if (isDragingEnter)
- {
- var files = (string[])e.Data.GetData(DataFormats.FileDrop);
- Array.Reverse(files);
- foreach(string file in files)
- {
- System.IO.FileInfo info = new System.IO.FileInfo(file);
- if(System.IO.Path.GetExtension(file).ToLower()==".pdf"&&info.Length>0)
- {
- int index = InsertIndex == -1 ? 0 : InsertIndex;
- viewModel.InsertFromFile(index, file);
- viewModel.ReloadAfterOption(true,true,new Tuple<int, int>(0,ListPageEdit.Items.Count));
- }
- }
- //其他文件 则新增一个页签打开
- // DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个 Load(TPDFDocument)的接口
- LineInset.Visibility = Visibility.Collapsed;
- ImgPicture.Visibility = Visibility.Collapsed;
- isDragingEnter = false;
- return;
- }
- #endregion
- var pos = e.GetPosition(ListPageEdit);
- var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
- //if (result == null)
- //{
- // //超出当前可控区域
- // ExitDraging();
- // return;
- //}
- ////查找元数据
- //var sourcePerson = e.Data.GetData(typeof(StackPanel)) as StackPanel;
- //if (sourcePerson == null)
- //{
- // ExitDraging();
- // return;
- //}
- //查找目标数据
- int targetindex = 0;//目标插入位置
- if (InsertIndex != -1)
- {
- //往前移动时 此index 不是准确的,需要处理++
- targetindex = InsertIndex;
- }
- //else//基本不会命中 仅作为保险措施
- //{
- // var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- // if (listBoxItem == null)
- // {
- // ////鼠标停留在两个item之间或其他无效区域 暂时不做处理(比较麻烦)
- // ExitDraging();
- // return;
- // }
- // var targetPerson = listBoxItem;
- // targetPerson.Opacity = 1;
- // sourcePerson.Opacity = 1;
- // if (ReferenceEquals(targetPerson, sourcePerson))
- // {
- // ExitDraging();
- // return;
- // }
- // targetindex = ListPageEdit.Items.IndexOf(targetPerson);
- //}
- List<ListBoxItem> list = new List<ListBoxItem>();
- List<int> sourceindex = new List<int>();//需要保存每个页面对应的位置
- //开始排序
- List<int> pages = new List<int>();
- //要先对所有选中项 根据页码排序
- for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
- {
- var pageindex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[i] as PageEditItem);
- pages.Add(pageindex);//存入的为页码索引值
- }
- if (pages.Count <= 0)
- {
- ExitDraging();
- return;
- }
- viewModel.DragToSort(targetindex,pages);
- ExitDraging();
- isDraging = false;
- }
- private void MidLane_Drop(object sender, DragEventArgs e)
- {
- ListPageEdit_Drop(sender, e);
- }
- private void ImgPicture_Drop(object sender, DragEventArgs e)
- {
- ListPageEdit_Drop(sender, e);
- }
- /// <summary>
- /// 拖拽事件写在外部的Grid里,会更加流畅
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Grid_DragOver(object sender, DragEventArgs e)
- {
- try
- {
- //ctrl建按下 不显示插入标记和虚影 因为释放不会响应drop事件
- if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
- return;
- //滚动后有 位置不准确 要减去滚动偏移量
- //控制线的位置
- var pos = e.GetPosition(ListPageEdit);
- var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
- if (result == null)
- {
- //MidLane.Visibility = Visibility.Collapsed;
- //return;
- }
- //获取当前鼠标指针下的容器
- var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (listBoxItem == null)
- {
- //MidLane.Visibility = Visibility.Collapsed;
- //return;
- }
- #region 计算虚影位置
- //xaml层 要设置 虚影控件为左上
- double xPos = 0;
- double yPos = 0;
- //内部拖动
- if (!isDragingEnter)
- {
- if(tempItem!=null)
- {
- ImgPicture.Width = tempItem.ItemSize.Width;
- ImgPicture.Height = tempItem.ItemSize.Height;
- ImgPicture.Source = tempItem.Image;
- xPos = e.GetPosition(ListPageEdit).X - item_x;
- yPos = e.GetPosition(ListPageEdit).Y - item_y;
- }
- }
- else
- {
- DragDropHelper.DragOver(this, e);
- //从外部拖入的逻辑
- //var pic = ToBitmapSource(dragingEnterPath);
- //ShadowPicture.Width = pic.Width;
- //ShadowPicture.Height = pic.Height;
- //ShadowPicture.Source = pic;
- //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
- //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
- }
- ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
- #endregion
- #region 计算插入标记位置
- var scroll = GetScrollHost(ListPageEdit);
- if (listBoxItem != null)
- {
- //虚拟化影响到该值计算
- var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
- LineInset.Visibility = Visibility.Visible;
- if(!isDragingEnter)
- {
- ImgPicture.Visibility = Visibility.Visible;
- }
- else
- {
- ImgPicture.Visibility = Visibility.Collapsed;
- }
- var panel = GetWrapPanel(ListPageEdit);
- //var item = panel.ItemSize.Width;
- //var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
- var item = listBoxItem.DesiredSize.Width;
- int count = (int)(panel.ViewportWidth / item);
- var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
- LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
- if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
- {
- LineInset.X2 = LineInset.X1 = p.X - gap / 2;
- InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
- }
- else
- {
- InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
- }
- Console.WriteLine("InsertIndex:{0}\r\n",InsertIndex);
- //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
- LineInset.Y1 = p.Y;
- if (LineInset.Y1 < 0)//避免超出上边界
- {
- LineInset.Y1 = 0;
- }
- //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
- LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
- if (LineInset.Y2 < 0)
- {
- LineInset.Y2 = 0;
- }
- }
- #endregion
- //暂时处理 鼠标移出边框时,虚影的显示问题
- if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
- {
- LineInset.Visibility = Visibility.Collapsed;
- ImgPicture.Visibility = Visibility.Collapsed;
- needScroll = false;
- }
- if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
- {
- LineInset.Visibility = Visibility.Collapsed;
- ImgPicture.Visibility = Visibility.Collapsed;
- needScroll = false;
- }
- #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
- speed = 0;
- if (pos.Y >= ListPageEdit.ActualHeight - 30)
- {
- speed = 30 - (int)(ListPageEdit.ActualHeight - pos.Y);
- needScroll = true;
- }
- else if (pos.Y <= 30)
- {
- speed = (int)(pos.Y - 30);
- needScroll = true;
- }
- else
- needScroll = false;
- var v = scroll.VerticalOffset;
- scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
- #endregion
- }
- catch
- {
- }
- }
- private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- //e.Handled = true;
- }
- private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- if (e.ChangedButton == MouseButton.Left)
- {
- //双击回到PDFViewer界面
- var item = sender as ListBoxItem;
- if (item != null)
- {
- var index = ListPageEdit.ItemContainerGenerator.IndexFromContainer(item);
- //双击事件无法绑定Command 暂时采用这种方式调用VM里的方法
- viewModel.BackToPDFViewer(index);
- }
- }
- }
- private void PageEdit_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- //BOTA缩略图模式下需要调整Item宽度
- if (GridBOTAHeader.Visibility == Visibility.Visible)
- {
- ItemSuitAcutalWidth(e.NewSize.Width);
- }
- }
- private void ItemSuitAcutalWidth(double width)
- {
- //缩略图模式下,保持一列或者两列显示
- if ((bool)TbnTwoLine.IsChecked)
- {
- var itemwidth = (width - 80) / 2;
- var itemHeight = 294 * itemwidth / 208.0;
- viewModel.ChangeItemSize(new Size(itemwidth, itemHeight));
- }
- else
- {
- var itemwidth = 0.6 * width;
- var itemheight = 294 * itemwidth / 208.0;
- viewModel.ChangeItemSize(new Size(itemwidth, itemheight));
- }
- }
- private void TbnTwoLine_Click(object sender, RoutedEventArgs e)
- {
- ItemSuitAcutalWidth(this.ActualWidth);
- }
- /// <summary>
- /// 用来判断是否有外界拖入的事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
- {
- DragDropHelper.DragEnter(this,e);
- var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
- if (file == null)//为null 表示内部拖动 触发的
- {
- return;
- }
- isDragingEnter = false;
- foreach (string f in file)
- {
- System.IO.FileInfo info = new System.IO.FileInfo(f);
- //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
- if(System.IO.Path.GetExtension(f).ToLower()==".pdf" &&info.Length>0)
- {
- isDragingEnter = true;
- }
- }
- }
- private void Grid_DragLeave(object sender, DragEventArgs e)
- {
- DragDropHelper.DragLeave();
- }
- private void Grid_Drop(object sender, DragEventArgs e)
- {
- DragDropHelper.Drop(this,e);
- }
- private void ListBoxItem_DragLeave(object sender, DragEventArgs e)
- {
- //增加辅助判断,防止误触发 拖动排序
- //较大幅度拖动才能触发排序
- isDraging = true;
- }
- }
- }
|