PageEditContent.xaml.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. if((bool)TbnTwoLine.IsChecked)
  195. {
  196. ///双列模式下要计算两列item
  197. range = new Tuple<int, int, int>(range.Item1,range.Item2*2,range.Item3);
  198. }
  199. eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
  200. }
  201. /// <summary>
  202. /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
  203. /// 返回值为页面范围 从1开始
  204. /// </summary>
  205. /// <param name="view"></param>
  206. /// <param name="itemSize"></param>
  207. /// <param name="itemMargin"></param>
  208. /// <returns></returns>
  209. private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  210. {
  211. //var scrollViewer = GetScrollHost(view);
  212. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  213. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  214. return new Tuple<int, int, int>(0, 0, 0);
  215. try
  216. {
  217. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  218. var currentWidth = scrollViewer.ActualWidth;
  219. //计算当前窗口大小能显示的行数和列数
  220. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  221. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  222. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  223. preItemCount = preItemCount * columnCount;
  224. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  225. preEnd = preEnd * columnCount + columnCount - 1;
  226. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  227. return new Tuple<int, int, int>(
  228. Math.Max(preItemCount, 0),
  229. Math.Min(view.Items.Count, preEnd),
  230. middle);
  231. }
  232. catch { }
  233. return new Tuple<int, int, int>(0, 0, 0);
  234. }
  235. /// <summary>
  236. /// 获取listbox里的ScrollViewer对象
  237. /// 留意如果有重写ListBox对象,该方法可能无效
  238. /// </summary>
  239. /// <param name="listBox"></param>
  240. /// <returns></returns>
  241. private ScrollViewer GetScrollHost(ListBox listBox)
  242. {
  243. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  244. {
  245. int s = VisualTreeHelper.GetChildrenCount(listBox);
  246. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  247. if (border != null)
  248. {
  249. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  250. }
  251. }
  252. return null;
  253. }
  254. #endregion
  255. /// <summary>
  256. /// 输入框显示时,自动获取焦点
  257. /// </summary>
  258. /// <param name="sender"></param>
  259. /// <param name="e"></param>
  260. private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  261. {
  262. if ((bool)e.NewValue)
  263. {
  264. (sender as Control).Focus();
  265. }
  266. }
  267. private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
  268. {
  269. try
  270. {
  271. if (e.LeftButton == MouseButtonState.Pressed)
  272. {
  273. //鼠标框选逻辑
  274. if (startChoose)
  275. {
  276. var position = e.GetPosition(ListPageEdit);
  277. if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
  278. {
  279. startChoose = false;
  280. RectChoose.Visibility = Visibility.Collapsed;
  281. //暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
  282. Mouse.Capture(null);
  283. return;
  284. }
  285. //矩形框内的item设为选中
  286. DoSelectItems();
  287. return;
  288. }
  289. //拖拽排序的逻辑
  290. var pos = e.GetPosition(ListPageEdit);
  291. if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
  292. {
  293. LineInset.Visibility = Visibility.Collapsed;
  294. ImgPicture.Visibility = Visibility.Collapsed;
  295. return;
  296. }
  297. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  298. if (result == null)
  299. {
  300. return;
  301. }
  302. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  303. if (listBoxItem == null)
  304. {
  305. return;
  306. }
  307. isDragingEnter = false;
  308. tempItem = listBoxItem.DataContext as PageEditItem;
  309. var item_pos = e.GetPosition(listBoxItem);
  310. if (item_pos != null)
  311. {
  312. item_x = item_pos.X;
  313. item_y = item_pos.Y;
  314. }
  315. var scroll = GetScrollHost(ListPageEdit);
  316. string filename = Guid.NewGuid().ToString() + ".pdf";
  317. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  318. System.IO.File.Create(tempPath);
  319. string[] files = new string[1];
  320. files[0] = tempPath;
  321. DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
  322. DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
  323. Mouse.Capture(ListPageEdit);
  324. return;
  325. }
  326. RectChoose.Visibility = Visibility.Collapsed;
  327. startChoose = false;
  328. Mouse.Capture(null);
  329. }
  330. catch
  331. {
  332. LineInset.Visibility = Visibility.Collapsed;
  333. ImgPicture.Visibility = Visibility.Collapsed;
  334. }
  335. }
  336. /// <summary>
  337. /// 判断是否开始框选、记录框选起始位置
  338. /// </summary>
  339. /// <param name="sender"></param>
  340. /// <param name="e"></param>
  341. private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  342. {
  343. var pos = e.GetPosition(ListPageEdit);
  344. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  345. if (result == null)
  346. {
  347. return;
  348. }
  349. //未选中item 并且不是点击滑轨时 开始框选
  350. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  351. var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
  352. if (listBoxItem == null)
  353. {
  354. if (scroller != null)
  355. {
  356. startChoose = false;
  357. return;
  358. }
  359. //点击空白处时开始框选
  360. startChoose = true;
  361. if (ListPageEdit.SelectedItems.Count > 0)
  362. {
  363. ListPageEdit.SelectedItems.Clear();
  364. }
  365. starPosition = e.GetPosition(ListPageEdit);
  366. starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
  367. Mouse.Capture(ListPageEdit);
  368. return;
  369. }
  370. //选中了item 时,不能框选
  371. startChoose = false;
  372. //更改系统的选中规则,选中状态下,鼠标松开后取消选中
  373. //方便实现多选拖拽功能
  374. if (listBoxItem.IsSelected == true && !Keyboard.IsKeyDown(Key.LeftCtrl))
  375. {
  376. e.Handled = true;
  377. }
  378. }
  379. private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  380. {
  381. var pos = e.GetPosition(ListPageEdit);
  382. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  383. if (result == null)
  384. {
  385. return;
  386. }
  387. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  388. if (listBoxItem == null)
  389. {
  390. return;
  391. }
  392. //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
  393. //拖拽框选之后的抬起鼠标不进入处理
  394. if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
  395. {
  396. ListPageEdit.SelectedItems.Clear();
  397. ListPageEdit.SelectedItem = listBoxItem;
  398. listBoxItem.IsSelected = true;
  399. return;
  400. }
  401. Mouse.Capture(null);
  402. //结束鼠标框选
  403. startChoose = false;
  404. RectChoose.Visibility = Visibility.Collapsed;
  405. }
  406. /// <summary>
  407. /// 获取Listobox的Wrappanel容器
  408. /// </summary>
  409. /// <param name="listBox"></param>
  410. /// <returns></returns>
  411. public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
  412. {
  413. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  414. var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
  415. return panel;
  416. }
  417. /// <summary>
  418. ///根据鼠标拖选的框 选中矩形框里面的Item
  419. /// </summary>
  420. private void DoSelectItems()
  421. {
  422. var s = GetScrollHost(ListPageEdit);
  423. Point start = new Point();
  424. //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
  425. start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
  426. var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
  427. RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
  428. RectChoose.Width = rec.Width;
  429. RectChoose.Height = rec.Height;
  430. RectChoose.Visibility = Visibility.Visible;
  431. //检测遍历所有项,筛选在矩形框中的Item
  432. for (int i = 0; i < ListPageEdit.Items.Count; i++)
  433. {
  434. var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  435. //通过这一步来避免重复误选中
  436. var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
  437. if (parent == null)
  438. continue;
  439. var v = VisualTreeHelper.GetOffset(_item);
  440. if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
  441. {
  442. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
  443. }
  444. else
  445. {
  446. ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
  447. }
  448. }
  449. return;
  450. }
  451. /// <summary>
  452. /// 退出拖拽模式
  453. /// </summary>
  454. private void ExitDraging()
  455. {
  456. LineInset.Visibility = Visibility.Collapsed;
  457. ImgPicture.Visibility = Visibility.Collapsed;
  458. isDraging = false;
  459. }
  460. /// <summary>
  461. /// 拖拽释放后的处理逻辑
  462. /// </summary>
  463. /// <param name="sender"></param>
  464. /// <param name="e"></param>
  465. private async void ListPageEdit_Drop(object sender, DragEventArgs e)
  466. {
  467. needScroll = false;
  468. if (!isDraging)
  469. {
  470. //未拖拽时隐藏插入标记和虚影
  471. LineInset.Visibility = Visibility.Collapsed;
  472. ImgPicture.Visibility = Visibility.Collapsed;
  473. return;
  474. }
  475. #region 功能付费锁
  476. // //if (!App.IsActive())
  477. // //{
  478. // // MidLane.Visibility = Visibility.Collapsed;
  479. // // IAPFunctionDialog dialog = new IAPFunctionDialog("PageEdit");
  480. // // dialog.ShowDialog();
  481. // // return;
  482. // //}
  483. #endregion
  484. #region 从外部拖拽插入文件
  485. if (isDragingEnter)
  486. {
  487. var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  488. Array.Reverse(files);
  489. foreach(string file in files)
  490. {
  491. System.IO.FileInfo info = new System.IO.FileInfo(file);
  492. if(System.IO.Path.GetExtension(file).ToLower()==".pdf"&&info.Length>0)
  493. {
  494. int index = InsertIndex == -1 ? 0 : InsertIndex;
  495. viewModel.InsertFromFile(index, file);
  496. viewModel.ReloadAfterOption(true,true,new Tuple<int, int>(0,ListPageEdit.Items.Count));
  497. }
  498. }
  499. //其他文件 则新增一个页签打开
  500. // DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个 Load(TPDFDocument)的接口
  501. LineInset.Visibility = Visibility.Collapsed;
  502. ImgPicture.Visibility = Visibility.Collapsed;
  503. isDragingEnter = false;
  504. return;
  505. }
  506. #endregion
  507. var pos = e.GetPosition(ListPageEdit);
  508. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  509. //if (result == null)
  510. //{
  511. // //超出当前可控区域
  512. // ExitDraging();
  513. // return;
  514. //}
  515. ////查找元数据
  516. //var sourcePerson = e.Data.GetData(typeof(StackPanel)) as StackPanel;
  517. //if (sourcePerson == null)
  518. //{
  519. // ExitDraging();
  520. // return;
  521. //}
  522. //查找目标数据
  523. int targetindex = 0;//目标插入位置
  524. if (InsertIndex != -1)
  525. {
  526. //往前移动时 此index 不是准确的,需要处理++
  527. targetindex = InsertIndex;
  528. }
  529. //else//基本不会命中 仅作为保险措施
  530. //{
  531. // var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  532. // if (listBoxItem == null)
  533. // {
  534. // ////鼠标停留在两个item之间或其他无效区域 暂时不做处理(比较麻烦)
  535. // ExitDraging();
  536. // return;
  537. // }
  538. // var targetPerson = listBoxItem;
  539. // targetPerson.Opacity = 1;
  540. // sourcePerson.Opacity = 1;
  541. // if (ReferenceEquals(targetPerson, sourcePerson))
  542. // {
  543. // ExitDraging();
  544. // return;
  545. // }
  546. // targetindex = ListPageEdit.Items.IndexOf(targetPerson);
  547. //}
  548. List<ListBoxItem> list = new List<ListBoxItem>();
  549. List<int> sourceindex = new List<int>();//需要保存每个页面对应的位置
  550. //开始排序
  551. List<int> pages = new List<int>();
  552. //要先对所有选中项 根据页码排序
  553. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  554. {
  555. var pageindex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[i] as PageEditItem);
  556. pages.Add(pageindex);//存入的为页码索引值
  557. }
  558. if (pages.Count <= 0)
  559. {
  560. ExitDraging();
  561. return;
  562. }
  563. viewModel.DragToSort(targetindex,pages);
  564. ExitDraging();
  565. isDraging = false;
  566. }
  567. private void MidLane_Drop(object sender, DragEventArgs e)
  568. {
  569. ListPageEdit_Drop(sender, e);
  570. }
  571. private void ImgPicture_Drop(object sender, DragEventArgs e)
  572. {
  573. ListPageEdit_Drop(sender, e);
  574. }
  575. /// <summary>
  576. /// 拖拽事件写在外部的Grid里,会更加流畅
  577. /// </summary>
  578. /// <param name="sender"></param>
  579. /// <param name="e"></param>
  580. private void Grid_DragOver(object sender, DragEventArgs e)
  581. {
  582. try
  583. {
  584. //ctrl建按下 不显示插入标记和虚影 因为释放不会响应drop事件
  585. if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
  586. return;
  587. //滚动后有 位置不准确 要减去滚动偏移量
  588. //控制线的位置
  589. var pos = e.GetPosition(ListPageEdit);
  590. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  591. if (result == null)
  592. {
  593. //MidLane.Visibility = Visibility.Collapsed;
  594. //return;
  595. }
  596. //获取当前鼠标指针下的容器
  597. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  598. if (listBoxItem == null)
  599. {
  600. //MidLane.Visibility = Visibility.Collapsed;
  601. //return;
  602. }
  603. #region 计算虚影位置
  604. //xaml层 要设置 虚影控件为左上
  605. double xPos = 0;
  606. double yPos = 0;
  607. //内部拖动
  608. if (!isDragingEnter)
  609. {
  610. if(tempItem!=null)
  611. {
  612. ImgPicture.Width = tempItem.ItemSize.Width;
  613. ImgPicture.Height = tempItem.ItemSize.Height;
  614. ImgPicture.Source = tempItem.Image;
  615. xPos = e.GetPosition(ListPageEdit).X - item_x;
  616. yPos = e.GetPosition(ListPageEdit).Y - item_y;
  617. }
  618. }
  619. else
  620. {
  621. DragDropHelper.DragOver(this, e);
  622. //从外部拖入的逻辑
  623. //var pic = ToBitmapSource(dragingEnterPath);
  624. //ShadowPicture.Width = pic.Width;
  625. //ShadowPicture.Height = pic.Height;
  626. //ShadowPicture.Source = pic;
  627. //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
  628. //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
  629. }
  630. ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
  631. #endregion
  632. #region 计算插入标记位置
  633. var scroll = GetScrollHost(ListPageEdit);
  634. if (listBoxItem != null)
  635. {
  636. //虚拟化影响到该值计算
  637. var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
  638. LineInset.Visibility = Visibility.Visible;
  639. if(!isDragingEnter)
  640. {
  641. ImgPicture.Visibility = Visibility.Visible;
  642. }
  643. else
  644. {
  645. ImgPicture.Visibility = Visibility.Collapsed;
  646. }
  647. var panel = GetWrapPanel(ListPageEdit);
  648. //var item = panel.ItemSize.Width;
  649. //var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
  650. var item = listBoxItem.DesiredSize.Width;
  651. int count = (int)(panel.ViewportWidth / item);
  652. var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
  653. LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
  654. if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
  655. {
  656. LineInset.X2 = LineInset.X1 = p.X - gap / 2;
  657. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
  658. }
  659. else
  660. {
  661. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
  662. }
  663. Console.WriteLine("InsertIndex:{0}\r\n",InsertIndex);
  664. //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
  665. LineInset.Y1 = p.Y;
  666. if (LineInset.Y1 < 0)//避免超出上边界
  667. {
  668. LineInset.Y1 = 0;
  669. }
  670. //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
  671. LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
  672. if (LineInset.Y2 < 0)
  673. {
  674. LineInset.Y2 = 0;
  675. }
  676. }
  677. #endregion
  678. //暂时处理 鼠标移出边框时,虚影的显示问题
  679. if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
  680. {
  681. LineInset.Visibility = Visibility.Collapsed;
  682. ImgPicture.Visibility = Visibility.Collapsed;
  683. needScroll = false;
  684. }
  685. if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
  686. {
  687. LineInset.Visibility = Visibility.Collapsed;
  688. ImgPicture.Visibility = Visibility.Collapsed;
  689. needScroll = false;
  690. }
  691. #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  692. speed = 0;
  693. if (pos.Y >= ListPageEdit.ActualHeight - 30)
  694. {
  695. speed = 30 - (int)(ListPageEdit.ActualHeight - pos.Y);
  696. needScroll = true;
  697. }
  698. else if (pos.Y <= 30)
  699. {
  700. speed = (int)(pos.Y - 30);
  701. needScroll = true;
  702. }
  703. else
  704. needScroll = false;
  705. var v = scroll.VerticalOffset;
  706. scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
  707. #endregion
  708. }
  709. catch
  710. {
  711. }
  712. }
  713. private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  714. {
  715. //e.Handled = true;
  716. }
  717. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  718. {
  719. if (e.ChangedButton == MouseButton.Left)
  720. {
  721. //双击回到PDFViewer界面
  722. var item = sender as ListBoxItem;
  723. if (item != null)
  724. {
  725. var index = ListPageEdit.ItemContainerGenerator.IndexFromContainer(item);
  726. //双击事件无法绑定Command 暂时采用这种方式调用VM里的方法
  727. viewModel.BackToPDFViewer(index);
  728. }
  729. }
  730. }
  731. private void PageEdit_SizeChanged(object sender, SizeChangedEventArgs e)
  732. {
  733. //BOTA缩略图模式下需要调整Item宽度
  734. if (GridBOTAHeader.Visibility == Visibility.Visible)
  735. {
  736. ItemSuitAcutalWidth(e.NewSize.Width);
  737. }
  738. }
  739. private void ItemSuitAcutalWidth(double width)
  740. {
  741. //缩略图模式下,保持一列或者两列显示
  742. if ((bool)TbnTwoLine.IsChecked)
  743. {
  744. var itemwidth = (width - 140) / 2;
  745. if(itemwidth<0)
  746. {
  747. itemwidth = width / 4;
  748. }
  749. var itemHeight = 294 * itemwidth / 208.0;
  750. viewModel.ChangeItemSize(new Size(itemwidth, itemHeight));
  751. }
  752. else
  753. {
  754. var itemwidth = 0.6 * width;
  755. var itemheight = 294 * itemwidth / 208.0;
  756. viewModel.ChangeItemSize(new Size(itemwidth, itemheight));
  757. }
  758. }
  759. private void TbnTwoLine_Click(object sender, RoutedEventArgs e)
  760. {
  761. ItemSuitAcutalWidth(this.ActualWidth);
  762. PulishEvent();
  763. }
  764. /// <summary>
  765. /// 用来判断是否有外界拖入的事件
  766. /// </summary>
  767. /// <param name="sender"></param>
  768. /// <param name="e"></param>
  769. private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
  770. {
  771. DragDropHelper.DragEnter(this,e);
  772. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  773. if (file == null)//为null 表示内部拖动 触发的
  774. {
  775. return;
  776. }
  777. isDragingEnter = false;
  778. foreach (string f in file)
  779. {
  780. System.IO.FileInfo info = new System.IO.FileInfo(f);
  781. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  782. if(System.IO.Path.GetExtension(f).ToLower()==".pdf" &&info.Length>0)
  783. {
  784. isDragingEnter = true;
  785. }
  786. }
  787. }
  788. private void Grid_DragLeave(object sender, DragEventArgs e)
  789. {
  790. DragDropHelper.DragLeave();
  791. }
  792. private void Grid_Drop(object sender, DragEventArgs e)
  793. {
  794. DragDropHelper.Drop(this,e);
  795. }
  796. private void ListBoxItem_DragLeave(object sender, DragEventArgs e)
  797. {
  798. //增加辅助判断,防止误触发 拖动排序
  799. //较大幅度拖动才能触发排序
  800. isDraging = true;
  801. }
  802. }
  803. }