PageEditContent.xaml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. using PDF_Office.EventAggregators;
  2. using PDF_Office.Helper;
  3. using PDF_Office.Model.PageEdit;
  4. using PDF_Office.ViewModels.PageEdit;
  5. using Prism.Events;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Controls.Primitives;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using System.Windows.Threading;
  22. using WpfToolkit.Controls;
  23. namespace PDF_Office.Views.PageEdit
  24. {
  25. /// <summary>
  26. /// PageEditContent.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class PageEditContent : UserControl
  29. {
  30. /// <summary>
  31. /// 用于通知刷新图片的事件
  32. /// 因为目前的图片刷新策略依赖很多UI事件判断,因为将主要判断逻辑放在UI层,VM负责图片刷新,非必要情况尽量不要采用这种形式
  33. /// </summary>
  34. private IEventAggregator eventor;
  35. /// <summary>
  36. /// 暂存的唯一索引值,用于区分多页签
  37. /// </summary>
  38. private string unicode;
  39. /// <summary>
  40. /// 是否是加载时第一次触发滚动
  41. /// </summary>
  42. private bool isFirstScrollChange = true;
  43. /// <summary>
  44. /// 用于判断滚轮停止的计时器
  45. /// </summary>
  46. private DispatcherTimer timer = new DispatcherTimer();
  47. /// <summary>
  48. /// 判断是否开始框选
  49. /// </summary>
  50. private bool startChoose = false;
  51. /// <summary>
  52. /// 框选的起始位置
  53. /// </summary>
  54. private Point starPosition = new Point();
  55. /// <summary>
  56. /// 记录当前滑块的状态
  57. /// </summary>
  58. private ScrollEventType scrolltype = ScrollEventType.EndScroll;
  59. //鼠标点击时在item中的位置 实现类似点哪拖哪的细节
  60. private double item_x;
  61. private double item_y;
  62. //插入标记代表的插入位置
  63. private int InsertIndex = -1;
  64. //拖动的Item
  65. private PageEditItem tempItem;
  66. /// <summary>
  67. /// 是否正在拖拽排序中,通过该变量避免单击触发拖动
  68. /// </summary>
  69. private bool isDraging = false;
  70. //是否正在从外部拖入文件
  71. public bool isDragingEnter { get; set; } = false;
  72. /// <summary>
  73. /// 是否需要自动滚动
  74. /// </summary>
  75. private bool needScroll = false;
  76. //自动滚动速度
  77. private int speed = 0;
  78. private PageEditContentViewModel viewModel;
  79. public PageEditContent()
  80. {
  81. InitializeComponent();
  82. }
  83. private void PageEditItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  84. {
  85. //BOTA缩略图里 插入子项时,刷新子项大小
  86. if (GridBOTAHeader.Visibility == Visibility.Visible&&e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
  87. {
  88. ItemSuitAcutalWidth(this.ActualWidth);
  89. }
  90. }
  91. public PageEditContent(IEventAggregator eventAggregator) : this()
  92. {
  93. eventor = eventAggregator;
  94. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  95. timer.Interval = TimeSpan.FromSeconds(0.3);
  96. timer.Tick += Timer_Tick;
  97. viewModel = this.DataContext as PageEditContentViewModel;
  98. viewModel.PageEditItems.CollectionChanged += PageEditItems_CollectionChanged;
  99. //订阅页面刷新事件
  100. eventor.GetEvent<PageEditNotifyEvent>().Subscribe(OneNotifyEvent, e => e.Unicode == unicode);
  101. }
  102. private void Timer_Tick(object sender, EventArgs e)
  103. {
  104. PulishEvent();
  105. timer.Stop();
  106. }
  107. private void OneNotifyEvent(PageEditNotifyEventArgs e)
  108. {
  109. switch (e.Type)
  110. {
  111. case NotifyType.RefreshPage:
  112. PulishEvent();
  113. break;
  114. case NotifyType.SelectItems:
  115. if (e.PageRange.Count == 1)
  116. {
  117. ListPageEdit.SelectedIndex = e.PageRange[0] - 1;
  118. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  119. }
  120. else
  121. {
  122. ListPageEdit.SelectedItems.Clear();
  123. for (int i = 0; i < e.PageRange.Count; i++)
  124. {
  125. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[e.PageRange[i] - 1]);
  126. }
  127. }
  128. break;
  129. }
  130. }
  131. #region UI事件
  132. /// <summary>
  133. /// 每次显示的时候就触发事件,刷新所有图片
  134. /// </summary>
  135. /// <param name="sender"></param>
  136. /// <param name="e"></param>
  137. private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  138. {
  139. if ((bool)e.NewValue)
  140. {
  141. //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
  142. PulishEvent();
  143. //当前页面发生变化时通过ScrollChanged事件来刷新图片
  144. isFirstScrollChange = true;
  145. if (GridBOTAHeader.Visibility == Visibility.Visible)
  146. {
  147. ItemSuitAcutalWidth(this.ActualWidth);
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// 鼠标滚轮滚动时触发的事件
  153. /// </summary>
  154. /// <param name="sender"></param>
  155. /// <param name="e"></param>
  156. private void ListPageEdit_ScrollChanged(object sender, ScrollChangedEventArgs e)
  157. {
  158. if (isFirstScrollChange)
  159. {
  160. PulishEvent();//第一次加载时触发的Scollchange 直接刷新界面,减少白板显示时间
  161. isFirstScrollChange = false;
  162. return;
  163. }
  164. else
  165. {
  166. if (e.VerticalChange != 0)
  167. timer.Start();//暂时找不到比较好的 判断Scroller停止的方法,先用计时器粗略判断
  168. }
  169. }
  170. /// <summary>
  171. /// 拖动右侧滑块时触发的事件
  172. /// </summary>
  173. /// <param name="sender"></param>
  174. /// <param name="e"></param>
  175. private void ListPageEdit_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
  176. {
  177. scrolltype = e.ScrollEventType;
  178. if (scrolltype != System.Windows.Controls.Primitives.ScrollEventType.EndScroll)
  179. {
  180. timer.Stop();
  181. return;
  182. }
  183. PulishEvent();
  184. }
  185. #endregion
  186. #region 方法
  187. /// <summary>
  188. /// 发布事件
  189. /// </summary>
  190. private void PulishEvent()
  191. {
  192. var itemSize = (ListPageEdit.Items[0] as PageEditItem).ItemSize;
  193. var range = GetRoughFromView(ListPageEdit, itemSize, new Thickness(5, 10, 5, 10));
  194. eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
  195. }
  196. /// <summary>
  197. /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
  198. /// 返回值为页面范围 从1开始
  199. /// </summary>
  200. /// <param name="view"></param>
  201. /// <param name="itemSize"></param>
  202. /// <param name="itemMargin"></param>
  203. /// <returns></returns>
  204. private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  205. {
  206. //var scrollViewer = GetScrollHost(view);
  207. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  208. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  209. return new Tuple<int, int, int>(0, 0, 0);
  210. try
  211. {
  212. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  213. var currentWidth = scrollViewer.ActualWidth;
  214. //计算当前窗口大小能显示的行数和列数
  215. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  216. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  217. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  218. preItemCount = preItemCount * columnCount;
  219. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  220. preEnd = preEnd * columnCount + columnCount - 1;
  221. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  222. return new Tuple<int, int, int>(
  223. Math.Max(preItemCount, 0),
  224. Math.Min(view.Items.Count, preEnd),
  225. middle);
  226. }
  227. catch { }
  228. return new Tuple<int, int, int>(0, 0, 0);
  229. }
  230. /// <summary>
  231. /// 获取listbox里的ScrollViewer对象
  232. /// 留意如果有重写ListBox对象,该方法可能无效
  233. /// </summary>
  234. /// <param name="listBox"></param>
  235. /// <returns></returns>
  236. private ScrollViewer GetScrollHost(ListBox listBox)
  237. {
  238. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  239. {
  240. int s = VisualTreeHelper.GetChildrenCount(listBox);
  241. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  242. if (border != null)
  243. {
  244. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  245. }
  246. }
  247. return null;
  248. }
  249. #endregion
  250. /// <summary>
  251. /// 输入框显示时,自动获取焦点
  252. /// </summary>
  253. /// <param name="sender"></param>
  254. /// <param name="e"></param>
  255. private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  256. {
  257. if ((bool)e.NewValue)
  258. {
  259. (sender as Control).Focus();
  260. }
  261. }
  262. private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
  263. {
  264. try
  265. {
  266. if (e.LeftButton == MouseButtonState.Pressed)
  267. {
  268. //鼠标框选逻辑
  269. if (startChoose)
  270. {
  271. var position = e.GetPosition(ListPageEdit);
  272. if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
  273. {
  274. startChoose = false;
  275. RectChoose.Visibility = Visibility.Collapsed;
  276. //暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
  277. Mouse.Capture(null);
  278. return;
  279. }
  280. //矩形框内的item设为选中
  281. DoSelectItems();
  282. return;
  283. }
  284. //拖拽排序的逻辑
  285. var pos = e.GetPosition(ListPageEdit);
  286. if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
  287. {
  288. LineInset.Visibility = Visibility.Collapsed;
  289. ImgPicture.Visibility = Visibility.Collapsed;
  290. return;
  291. }
  292. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  293. if (result == null)
  294. {
  295. return;
  296. }
  297. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  298. if (listBoxItem == null)
  299. {
  300. return;
  301. }
  302. isDragingEnter = false;
  303. tempItem = listBoxItem.DataContext as PageEditItem;
  304. var item_pos = e.GetPosition(listBoxItem);
  305. if (item_pos != null)
  306. {
  307. item_x = item_pos.X;
  308. item_y = item_pos.Y;
  309. }
  310. var scroll = GetScrollHost(ListPageEdit);
  311. string filename = Guid.NewGuid().ToString() + ".pdf";
  312. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  313. System.IO.File.Create(tempPath);
  314. string[] files = new string[1];
  315. files[0] = tempPath;
  316. DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
  317. DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
  318. Mouse.Capture(ListPageEdit);
  319. return;
  320. }
  321. RectChoose.Visibility = Visibility.Collapsed;
  322. startChoose = false;
  323. Mouse.Capture(null);
  324. }
  325. catch
  326. {
  327. LineInset.Visibility = Visibility.Collapsed;
  328. ImgPicture.Visibility = Visibility.Collapsed;
  329. }
  330. }
  331. /// <summary>
  332. /// 判断是否开始框选、记录框选起始位置
  333. /// </summary>
  334. /// <param name="sender"></param>
  335. /// <param name="e"></param>
  336. private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  337. {
  338. var pos = e.GetPosition(ListPageEdit);
  339. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  340. if (result == null)
  341. {
  342. return;
  343. }
  344. //未选中item 并且不是点击滑轨时 开始框选
  345. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  346. var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
  347. if (listBoxItem == null)
  348. {
  349. if (scroller != null)
  350. {
  351. startChoose = false;
  352. return;
  353. }
  354. //点击空白处时开始框选
  355. startChoose = true;
  356. if (ListPageEdit.SelectedItems.Count > 0)
  357. {
  358. ListPageEdit.SelectedItems.Clear();
  359. }
  360. starPosition = e.GetPosition(ListPageEdit);
  361. starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
  362. Mouse.Capture(ListPageEdit);
  363. return;
  364. }
  365. //选中了item 时,不能框选
  366. startChoose = false;
  367. //更改系统的选中规则,选中状态下,鼠标松开后取消选中
  368. //方便实现多选拖拽功能
  369. if (listBoxItem.IsSelected == true && !Keyboard.IsKeyDown(Key.LeftCtrl))
  370. {
  371. e.Handled = true;
  372. }
  373. }
  374. private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  375. {
  376. var pos = e.GetPosition(ListPageEdit);
  377. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  378. if (result == null)
  379. {
  380. return;
  381. }
  382. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  383. if (listBoxItem == null)
  384. {
  385. return;
  386. }
  387. //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
  388. //拖拽框选之后的抬起鼠标不进入处理
  389. if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
  390. {
  391. ListPageEdit.SelectedItems.Clear();
  392. ListPageEdit.SelectedItem = listBoxItem;
  393. listBoxItem.IsSelected = true;
  394. return;
  395. }
  396. Mouse.Capture(null);
  397. //结束鼠标框选
  398. startChoose = false;
  399. RectChoose.Visibility = Visibility.Collapsed;
  400. }
  401. /// <summary>
  402. /// 获取Listobox的Wrappanel容器
  403. /// </summary>
  404. /// <param name="listBox"></param>
  405. /// <returns></returns>
  406. public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
  407. {
  408. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  409. var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
  410. return panel;
  411. }
  412. /// <summary>
  413. ///根据鼠标拖选的框 选中矩形框里面的Item
  414. /// </summary>
  415. private void DoSelectItems()
  416. {
  417. var s = GetScrollHost(ListPageEdit);
  418. Point start = new Point();
  419. //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
  420. start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
  421. var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
  422. RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
  423. RectChoose.Width = rec.Width;
  424. RectChoose.Height = rec.Height;
  425. RectChoose.Visibility = Visibility.Visible;
  426. //检测遍历所有项,筛选在矩形框中的Item
  427. for (int i = 0; i < ListPageEdit.Items.Count; i++)
  428. {
  429. var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  430. //通过这一步来避免重复误选中
  431. var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
  432. if (parent == null)
  433. continue;
  434. var v = VisualTreeHelper.GetOffset(_item);
  435. if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
  436. {
  437. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
  438. }
  439. else
  440. {
  441. ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
  442. }
  443. }
  444. return;
  445. }
  446. /// <summary>
  447. /// 退出拖拽模式
  448. /// </summary>
  449. private void ExitDraging()
  450. {
  451. LineInset.Visibility = Visibility.Collapsed;
  452. ImgPicture.Visibility = Visibility.Collapsed;
  453. isDraging = false;
  454. }
  455. /// <summary>
  456. /// 拖拽释放后的处理逻辑
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private async void ListPageEdit_Drop(object sender, DragEventArgs e)
  461. {
  462. needScroll = false;
  463. if (!isDraging)
  464. {
  465. //未拖拽时隐藏插入标记和虚影
  466. LineInset.Visibility = Visibility.Collapsed;
  467. ImgPicture.Visibility = Visibility.Collapsed;
  468. return;
  469. }
  470. #region 功能付费锁
  471. // //if (!App.IsActive())
  472. // //{
  473. // // MidLane.Visibility = Visibility.Collapsed;
  474. // // IAPFunctionDialog dialog = new IAPFunctionDialog("PageEdit");
  475. // // dialog.ShowDialog();
  476. // // return;
  477. // //}
  478. #endregion
  479. #region 从外部拖拽插入文件
  480. if (isDragingEnter)
  481. {
  482. var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  483. Array.Reverse(files);
  484. foreach(string file in files)
  485. {
  486. System.IO.FileInfo info = new System.IO.FileInfo(file);
  487. if(System.IO.Path.GetExtension(file).ToLower()==".pdf"&&info.Length>0)
  488. {
  489. int index = InsertIndex == -1 ? 0 : InsertIndex;
  490. viewModel.InsertFromFile(index, file);
  491. viewModel.ReloadAfterOption(true,true,new Tuple<int, int>(0,ListPageEdit.Items.Count));
  492. }
  493. }
  494. //其他文件 则新增一个页签打开
  495. // DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个 Load(TPDFDocument)的接口
  496. LineInset.Visibility = Visibility.Collapsed;
  497. ImgPicture.Visibility = Visibility.Collapsed;
  498. isDragingEnter = false;
  499. return;
  500. }
  501. #endregion
  502. var pos = e.GetPosition(ListPageEdit);
  503. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  504. //if (result == null)
  505. //{
  506. // //超出当前可控区域
  507. // ExitDraging();
  508. // return;
  509. //}
  510. ////查找元数据
  511. //var sourcePerson = e.Data.GetData(typeof(StackPanel)) as StackPanel;
  512. //if (sourcePerson == null)
  513. //{
  514. // ExitDraging();
  515. // return;
  516. //}
  517. //查找目标数据
  518. int targetindex = 0;//目标插入位置
  519. if (InsertIndex != -1)
  520. {
  521. //往前移动时 此index 不是准确的,需要处理++
  522. targetindex = InsertIndex;
  523. }
  524. //else//基本不会命中 仅作为保险措施
  525. //{
  526. // var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  527. // if (listBoxItem == null)
  528. // {
  529. // ////鼠标停留在两个item之间或其他无效区域 暂时不做处理(比较麻烦)
  530. // ExitDraging();
  531. // return;
  532. // }
  533. // var targetPerson = listBoxItem;
  534. // targetPerson.Opacity = 1;
  535. // sourcePerson.Opacity = 1;
  536. // if (ReferenceEquals(targetPerson, sourcePerson))
  537. // {
  538. // ExitDraging();
  539. // return;
  540. // }
  541. // targetindex = ListPageEdit.Items.IndexOf(targetPerson);
  542. //}
  543. List<ListBoxItem> list = new List<ListBoxItem>();
  544. List<int> sourceindex = new List<int>();//需要保存每个页面对应的位置
  545. //开始排序
  546. List<int> pages = new List<int>();
  547. //要先对所有选中项 根据页码排序
  548. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  549. {
  550. var pageindex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[i] as PageEditItem);
  551. pages.Add(pageindex);//存入的为页码索引值
  552. }
  553. if (pages.Count <= 0)
  554. {
  555. ExitDraging();
  556. return;
  557. }
  558. viewModel.DragToSort(targetindex,pages);
  559. ExitDraging();
  560. isDraging = false;
  561. }
  562. private void MidLane_Drop(object sender, DragEventArgs e)
  563. {
  564. ListPageEdit_Drop(sender, e);
  565. }
  566. private void ImgPicture_Drop(object sender, DragEventArgs e)
  567. {
  568. ListPageEdit_Drop(sender, e);
  569. }
  570. /// <summary>
  571. /// 拖拽事件写在外部的Grid里,会更加流畅
  572. /// </summary>
  573. /// <param name="sender"></param>
  574. /// <param name="e"></param>
  575. private void Grid_DragOver(object sender, DragEventArgs e)
  576. {
  577. try
  578. {
  579. //ctrl建按下 不显示插入标记和虚影 因为释放不会响应drop事件
  580. if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
  581. return;
  582. //滚动后有 位置不准确 要减去滚动偏移量
  583. //控制线的位置
  584. var pos = e.GetPosition(ListPageEdit);
  585. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  586. if (result == null)
  587. {
  588. //MidLane.Visibility = Visibility.Collapsed;
  589. //return;
  590. }
  591. //获取当前鼠标指针下的容器
  592. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  593. if (listBoxItem == null)
  594. {
  595. //MidLane.Visibility = Visibility.Collapsed;
  596. //return;
  597. }
  598. #region 计算虚影位置
  599. //xaml层 要设置 虚影控件为左上
  600. double xPos = 0;
  601. double yPos = 0;
  602. //内部拖动
  603. if (!isDragingEnter)
  604. {
  605. if(tempItem!=null)
  606. {
  607. ImgPicture.Width = tempItem.ItemSize.Width;
  608. ImgPicture.Height = tempItem.ItemSize.Height;
  609. ImgPicture.Source = tempItem.Image;
  610. xPos = e.GetPosition(ListPageEdit).X - item_x;
  611. yPos = e.GetPosition(ListPageEdit).Y - item_y;
  612. }
  613. }
  614. else
  615. {
  616. DragDropHelper.DragOver(this, e);
  617. //从外部拖入的逻辑
  618. //var pic = ToBitmapSource(dragingEnterPath);
  619. //ShadowPicture.Width = pic.Width;
  620. //ShadowPicture.Height = pic.Height;
  621. //ShadowPicture.Source = pic;
  622. //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
  623. //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
  624. }
  625. ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
  626. #endregion
  627. #region 计算插入标记位置
  628. var scroll = GetScrollHost(ListPageEdit);
  629. if (listBoxItem != null)
  630. {
  631. //虚拟化影响到该值计算
  632. var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
  633. LineInset.Visibility = Visibility.Visible;
  634. if(!isDragingEnter)
  635. {
  636. ImgPicture.Visibility = Visibility.Visible;
  637. }
  638. else
  639. {
  640. ImgPicture.Visibility = Visibility.Collapsed;
  641. }
  642. var panel = GetWrapPanel(ListPageEdit);
  643. //var item = panel.ItemSize.Width;
  644. //var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
  645. var item = listBoxItem.DesiredSize.Width;
  646. int count = (int)(panel.ViewportWidth / item);
  647. var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
  648. LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
  649. if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
  650. {
  651. LineInset.X2 = LineInset.X1 = p.X - gap / 2;
  652. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
  653. }
  654. else
  655. {
  656. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
  657. }
  658. Console.WriteLine("InsertIndex:{0}\r\n",InsertIndex);
  659. //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
  660. LineInset.Y1 = p.Y;
  661. if (LineInset.Y1 < 0)//避免超出上边界
  662. {
  663. LineInset.Y1 = 0;
  664. }
  665. //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
  666. LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
  667. if (LineInset.Y2 < 0)
  668. {
  669. LineInset.Y2 = 0;
  670. }
  671. }
  672. #endregion
  673. //暂时处理 鼠标移出边框时,虚影的显示问题
  674. if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
  675. {
  676. LineInset.Visibility = Visibility.Collapsed;
  677. ImgPicture.Visibility = Visibility.Collapsed;
  678. needScroll = false;
  679. }
  680. if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
  681. {
  682. LineInset.Visibility = Visibility.Collapsed;
  683. ImgPicture.Visibility = Visibility.Collapsed;
  684. needScroll = false;
  685. }
  686. #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  687. speed = 0;
  688. if (pos.Y >= ListPageEdit.ActualHeight - 30)
  689. {
  690. speed = 30 - (int)(ListPageEdit.ActualHeight - pos.Y);
  691. needScroll = true;
  692. }
  693. else if (pos.Y <= 30)
  694. {
  695. speed = (int)(pos.Y - 30);
  696. needScroll = true;
  697. }
  698. else
  699. needScroll = false;
  700. var v = scroll.VerticalOffset;
  701. scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
  702. #endregion
  703. }
  704. catch
  705. {
  706. }
  707. }
  708. private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  709. {
  710. //e.Handled = true;
  711. }
  712. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  713. {
  714. if (e.ChangedButton == MouseButton.Left)
  715. {
  716. //双击回到PDFViewer界面
  717. var item = sender as ListBoxItem;
  718. if (item != null)
  719. {
  720. var index = ListPageEdit.ItemContainerGenerator.IndexFromContainer(item);
  721. //双击事件无法绑定Command 暂时采用这种方式调用VM里的方法
  722. viewModel.BackToPDFViewer(index);
  723. }
  724. }
  725. }
  726. private void PageEdit_SizeChanged(object sender, SizeChangedEventArgs e)
  727. {
  728. //BOTA缩略图模式下需要调整Item宽度
  729. if (GridBOTAHeader.Visibility == Visibility.Visible)
  730. {
  731. ItemSuitAcutalWidth(e.NewSize.Width);
  732. }
  733. }
  734. private void ItemSuitAcutalWidth(double width)
  735. {
  736. //缩略图模式下,保持一列或者两列显示
  737. if ((bool)TbnTwoLine.IsChecked)
  738. {
  739. var itemwidth = (width - 80) / 2;
  740. var itemHeight = 294 * itemwidth / 208.0;
  741. viewModel.ChangeItemSize(new Size(itemwidth, itemHeight));
  742. }
  743. else
  744. {
  745. var itemwidth = 0.6 * width;
  746. var itemheight = 294 * itemwidth / 208.0;
  747. viewModel.ChangeItemSize(new Size(itemwidth, itemheight));
  748. }
  749. }
  750. private void TbnTwoLine_Click(object sender, RoutedEventArgs e)
  751. {
  752. ItemSuitAcutalWidth(this.ActualWidth);
  753. }
  754. /// <summary>
  755. /// 用来判断是否有外界拖入的事件
  756. /// </summary>
  757. /// <param name="sender"></param>
  758. /// <param name="e"></param>
  759. private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
  760. {
  761. DragDropHelper.DragEnter(this,e);
  762. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  763. if (file == null)//为null 表示内部拖动 触发的
  764. {
  765. return;
  766. }
  767. isDragingEnter = false;
  768. foreach (string f in file)
  769. {
  770. System.IO.FileInfo info = new System.IO.FileInfo(f);
  771. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  772. if(System.IO.Path.GetExtension(f).ToLower()==".pdf" &&info.Length>0)
  773. {
  774. isDragingEnter = true;
  775. }
  776. }
  777. }
  778. private void Grid_DragLeave(object sender, DragEventArgs e)
  779. {
  780. DragDropHelper.DragLeave();
  781. }
  782. private void Grid_Drop(object sender, DragEventArgs e)
  783. {
  784. DragDropHelper.Drop(this,e);
  785. }
  786. private void ListBoxItem_DragLeave(object sender, DragEventArgs e)
  787. {
  788. //增加辅助判断,防止误触发 拖动排序
  789. //较大幅度拖动才能触发排序
  790. isDraging = true;
  791. }
  792. }
  793. }