PageEditContent.xaml.cs 34 KB

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