PDFToolsContent.xaml.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. using PDF_Master.CustomControl;
  2. using PDF_Master.Helper;
  3. using PDF_Master.Model.PDFTool;
  4. using PDF_Master.ViewModels.HomePanel;
  5. using PDF_Master.ViewModels.HomePanel.PDFTools;
  6. using PDFSettings;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Animation;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace PDF_Master.Views.HomePanel.PDFTools
  24. {
  25. /// <summary>
  26. /// ToolbarPage.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class PDFToolsContent : UserControl
  29. {
  30. private PDFToolsContentViewModel ViewModel => DataContext as PDFToolsContentViewModel;
  31. private ListItemQuickTool sourceListItem = null;
  32. private ListItemQuickTool targetListItem = null;
  33. private ListItemQuickTool quickSourceListItem = null;
  34. private List<ToolItem> quickTools = null;
  35. private List<ToolItem> allTools = null;
  36. private List<ToolItem> moreTools = null;
  37. PDFToolsHelper PDFtools;
  38. private string fileName = string.Empty;
  39. private BitmapSource bitmapSource = null;
  40. private bool isMoreCuts = false;
  41. private bool isMove = false;
  42. private Point wrpnlPoint;
  43. private Point mousePoint;
  44. public PDFToolsContent()
  45. {
  46. InitializeComponent();
  47. PDFtools = PDFToolsHelper.GetInstance();
  48. #region ListBox
  49. SetListBox();
  50. #endregion ListBox
  51. }
  52. //用来测试的,PDF工具还在优化中
  53. private void Button_Click(object sender, RoutedEventArgs e)
  54. {
  55. var PDFtools = Helper.PDFToolsHelper.GetInstance();
  56. PDFtools.ClearData();
  57. SetListBox();
  58. ListShortCutsExpend.Items.Refresh();
  59. ListMoreCutsExpend.Items.Refresh();
  60. ListBoxCutsWidthChanged(ListShortCutsExpend);
  61. ListBoxCutsWidthChanged(ListMoreCutsExpend);
  62. ListShortCuts.Items.Refresh();
  63. ListMoreCuts.Items.Refresh();
  64. ListBoxCutsWidthChanged(ListShortCuts);
  65. ListBoxCutsWidthChanged(ListMoreCuts);
  66. }
  67. /// <summary>
  68. /// 获取与窗口相对的鼠标位置。
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. private void OnRendering(object sender, EventArgs e)
  73. {
  74. mousePoint = Mouse.GetPosition(this);
  75. Console.WriteLine($"鼠标坐标:{mousePoint.X}:{mousePoint.Y}");
  76. }
  77. private void SetListBox()
  78. {
  79. quickTools = PDFtools.QuickTools;
  80. allTools = PDFtools.AllTools;
  81. moreTools = PDFtools.MoreTools;
  82. ListShortCutsExpend.ItemsSource = ListShortCuts.ItemsSource = quickTools;
  83. ListMoreCutsExpend.ItemsSource = ListMoreCuts.ItemsSource = moreTools;
  84. }
  85. private void Page_Loaded(object sender, RoutedEventArgs e)
  86. {
  87. UpdateExptend();
  88. if (ViewModel != null)
  89. {
  90. ViewModel.ExpendToolsHanlder -= ViewModel_ExpendToolsHanlder;
  91. ViewModel.ExpendToolsHanlder += ViewModel_ExpendToolsHanlder;
  92. }
  93. }
  94. private void ViewModel_ExpendToolsHanlder(object sender, bool e)
  95. {
  96. if(e)
  97. {
  98. ListShortCutsExpend.Items.Refresh();
  99. ListMoreCutsExpend.Items.Refresh();
  100. ListBoxCutsWidthChanged(ListShortCutsExpend);
  101. ListBoxCutsWidthChanged(ListMoreCutsExpend);
  102. }
  103. else
  104. {
  105. ListShortCuts.Items.Refresh();
  106. ListMoreCuts.Items.Refresh();
  107. ListBoxCutsWidthChanged(ListShortCuts);
  108. ListBoxCutsWidthChanged(ListMoreCuts);
  109. }
  110. }
  111. private ListBoxEx GetListShortControl()
  112. {
  113. ListBoxEx ShortEx = null;
  114. if (ListShortCuts.Visibility == Visibility.Visible)
  115. {
  116. ShortEx = ListShortCuts;
  117. }
  118. else
  119. {
  120. ShortEx = ListShortCutsExpend;
  121. }
  122. return ShortEx;
  123. }
  124. private ListBoxEx GetListMoreControl()
  125. {
  126. ListBoxEx MoreEx = null;
  127. if (ListMoreCuts.Visibility == Visibility.Visible)
  128. {
  129. MoreEx = ListMoreCuts;
  130. }
  131. else
  132. {
  133. MoreEx = ListMoreCutsExpend;
  134. }
  135. return MoreEx;
  136. }
  137. private void ListBoxMoreCuts_PreviewMouseMove(object sender, MouseEventArgs e)
  138. {
  139. ListBoxEx MoreEx = GetListMoreControl();
  140. if (MoreEx == null) return;
  141. var pos = e.GetPosition(MoreEx);
  142. if (e.LeftButton == MouseButtonState.Pressed)
  143. {
  144. MoreEx.AllowDrop = true;
  145. HitTestResult result = VisualTreeHelper.HitTest(MoreEx, pos);
  146. if (result == null)
  147. {
  148. sourceImage.Visibility = Visibility.Hidden;
  149. return;
  150. }
  151. sourceListItem = (ListItemQuickTool)CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  152. if (sourceListItem == null || sourceListItem.Content != MoreEx.SelectedItem)
  153. {
  154. return;
  155. }
  156. //获取控件相对于另一控件的坐标
  157. wrpnlPoint = sourceListItem.TranslatePoint(new Point(), this);
  158. System.Windows.DataObject dataObj = new System.Windows.DataObject(sourceListItem.Content as ToolItem);
  159. if (dataObj != null)
  160. {
  161. //控件截图
  162. IsMoreToolbars();
  163. if (string.IsNullOrEmpty(fileName))
  164. {
  165. sourceImage.Visibility = Visibility.Hidden;
  166. return;
  167. }
  168. //图片解码
  169. ImageDecoding();
  170. isMoreCuts = true;
  171. DragDrop.DoDragDrop(MoreEx, dataObj, System.Windows.DragDropEffects.Move);
  172. }
  173. }
  174. else
  175. {
  176. moveImage.Visibility = Visibility.Hidden;
  177. sourceImage.Visibility = Visibility.Hidden;
  178. }
  179. }
  180. private void ImageDecoding()
  181. {
  182. Stream imageStreamSource = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  183. PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
  184. bitmapSource = decoder.Frames[0];
  185. }
  186. private void IsMoreToolbars()
  187. {
  188. var source = sourceListItem.Content as ToolItem;
  189. if (moreTools == null && moreTools.Count != 0) return;
  190. List<ToolItem> toolModule = moreTools.FindAll(item => item.FnName == source.FnName);
  191. if (toolModule.Count > 0)
  192. {
  193. // 判断文件夹是否存在,不存在则创建
  194. DirectoryInfo folder = new DirectoryInfo(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Asource"));
  195. if (!folder.Exists)
  196. {
  197. folder.Create();
  198. }
  199. fileName = System.IO.Path.Combine(folder.FullName, $"{source.FnName}.png");
  200. SaveToPng(sourceListItem, fileName);
  201. }
  202. }
  203. private void ListBoxShortCuts_Drop(object sender, DragEventArgs e)
  204. {
  205. ListBoxEx ShortEx = GetListShortControl();
  206. if(ShortEx == null) return;
  207. var pos = e.GetPosition(ShortEx);
  208. var result = VisualTreeHelper.HitTest(ShortEx, pos);
  209. if (result == null)
  210. {
  211. WorkingWithAnimation(pos);
  212. return;
  213. }
  214. //查找目标数据
  215. targetListItem = (ListItemQuickTool)CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  216. if (targetListItem == null)
  217. {
  218. WorkingWithAnimation(pos);
  219. return;
  220. }
  221. var targetAToolModule = targetListItem.Content as ToolItem;
  222. ToolItem target = quickTools.Find(c => c.FnName == targetAToolModule.FnName);
  223. //查找元数据
  224. var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
  225. if (sourceAToolModule == null)
  226. {
  227. sourceImage.Visibility = Visibility.Hidden;
  228. // target.SetValue(AToolModule.IsFilePickedProperty, false);
  229. return;
  230. }
  231. if (ReferenceEquals(targetAToolModule, sourceAToolModule))
  232. {
  233. sourceImage.Visibility = Visibility.Hidden;
  234. // target.SetValue(AToolModule.IsFilePickedProperty, false);
  235. return;
  236. }
  237. if (targetAToolModule == null && sourceAToolModule == null)
  238. {
  239. sourceImage.Visibility = Visibility.Hidden;
  240. // target.SetValue(AToolModule.IsFilePickedProperty, false);
  241. return;
  242. }
  243. ModuleExchange(sourceAToolModule, targetAToolModule);
  244. }
  245. private void ModuleExchange(ToolItem sourceAToolModule, ToolItem targetAToolModule)
  246. {
  247. if (moreTools == null) return;
  248. ToolItem source = moreTools.Find(c => c.FnName == sourceAToolModule.FnName);
  249. ListBoxEx MoreEx = GetListMoreControl();
  250. if (MoreEx == null) return;
  251. ListBoxEx ShortEx = GetListShortControl();
  252. if (ShortEx == null) return;
  253. int targetIndex = quickTools.FindIndex(item => item.FnName == targetAToolModule.FnName);
  254. if (targetIndex < 0) return;
  255. if (!isMoreCuts)
  256. {
  257. MoreEx.AllowDrop = false;
  258. sourceImage.Visibility = Visibility.Hidden;
  259. source = quickTools.Find(c => c.FnName == sourceAToolModule.FnName);
  260. if (source == null) return;
  261. int sourceIndex = quickTools.FindIndex(item => item.FnName == sourceAToolModule.FnName);
  262. if (sourceIndex < 0) return;
  263. var temp = quickTools[sourceIndex];
  264. quickTools[sourceIndex] = quickTools[targetIndex];
  265. quickTools[targetIndex] = temp;
  266. var sourceId = source.ToolTypeId;
  267. var targetId = target.ToolTypeId;
  268. source.ToolTypeId = targetId;
  269. target.ToolTypeId = sourceId;
  270. source.IsQuickTool = true;
  271. target.IsQuickTool = true;
  272. ShortEx.Items.Refresh();
  273. ListBoxCutsWidthChanged(ShortEx);
  274. }
  275. else
  276. {
  277. if (target == null) return;
  278. if (source == null) return;
  279. moreTools.Remove(source);
  280. moreTools.Insert(0, target);
  281. quickTools.Remove(target);
  282. quickTools.Insert(targetIndex, source);
  283. source.IsQuickTool = true;
  284. target.IsQuickTool = false;
  285. var targetId = target.ToolTypeId;
  286. source.ToolTypeId = targetId;
  287. target.ToolTypeId = 0;//移到第一个位置
  288. MoreEx.Items.Refresh();
  289. ShortEx.Items.Refresh();
  290. sourceImage.Visibility = Visibility.Hidden;
  291. targetListItem.IsOverModular = false;
  292. ListBoxCutsWidthChanged(ShortEx);
  293. ListBoxCutsWidthChanged(MoreEx);
  294. }
  295. if (PDFtools != null)
  296. PDFtools.SaveCacheList();
  297. }
  298. private void WorkingWithAnimation(Point pos)
  299. {
  300. sourceImage.Visibility = Visibility.Hidden;
  301. if (!isMoreCuts || !isMove)
  302. {
  303. moveImage.Visibility = Visibility.Hidden;
  304. }
  305. else
  306. {
  307. if (sourceListItem == null) return;
  308. moveImage.Visibility = Visibility.Visible;
  309. Point point = sourceListItem.TransformToAncestor(this).Transform(new Point(0, 0));
  310. ImageMoveTo(new Point(pos.X - 80, pos.Y), new Point(wrpnlPoint.X + 40, wrpnlPoint.Y), moveImage, 200);
  311. }
  312. }
  313. /// <summary>
  314. /// 图片移动动画
  315. /// </summary>
  316. /// <param name="curPoint">初始位置</param>
  317. /// <param name="deskPoint">控件要移动到的位置</param>
  318. /// <param name="ell">你要移动的空间</param>
  319. /// <param name="space">设置移动的时间片(关系到控件移动的速度)</param>
  320. private void ImageMoveTo(Point curPoint, Point deskPoint, Image ell, double space)
  321. {
  322. Storyboard storyboard = new Storyboard(); //创建Storyboard对象
  323. double lxspeed = space, lyspeed = space; //设置X方向 / Y方向 移动时间片
  324. //创建X轴方向动画
  325. DoubleAnimation dox = new DoubleAnimation(
  326. curPoint.X,
  327. deskPoint.X,
  328. new Duration(TimeSpan.FromMilliseconds(lxspeed))
  329. );
  330. //创建Y轴方向动画
  331. DoubleAnimation doy = new DoubleAnimation(
  332. curPoint.Y,
  333. deskPoint.Y,
  334. new Duration(TimeSpan.FromMilliseconds(lyspeed))
  335. );
  336. Storyboard.SetTarget(dox, ell);
  337. Storyboard.SetTarget(doy, ell);
  338. Storyboard.SetTargetProperty(dox, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
  339. Storyboard.SetTargetProperty(doy, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
  340. storyboard.Children.Add(dox);
  341. storyboard.Children.Add(doy);
  342. storyboard.Completed += Storyboard_Completed;
  343. //动画播放
  344. storyboard.Begin();
  345. }
  346. private void Storyboard_Completed(object sender, EventArgs e)
  347. {
  348. moveImage.Visibility = Visibility.Hidden;
  349. }
  350. /// <summary>
  351. /// 控件截图
  352. /// </summary>
  353. /// <param name="visual">控件</param>
  354. /// <param name="fileName">文件路径 C:\Users\oyxh\AppData\Local\Temp\</param>
  355. private void SaveToPng(FrameworkElement visual, string fileName)
  356. {
  357. DrawingVisual drawingVisual = new DrawingVisual();
  358. using (DrawingContext context = drawingVisual.RenderOpen())
  359. {
  360. VisualBrush brush = new VisualBrush(sourceListItem) { Stretch = Stretch.None };
  361. context.DrawRectangle(brush, null, new Rect(0, 0, sourceListItem.ActualWidth, sourceListItem.ActualHeight));
  362. context.Close();
  363. }
  364. RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)sourceListItem.ActualWidth, (int)sourceListItem.ActualHeight, 96d, 96d, PixelFormats.Default);
  365. targetBitmap.Render(drawingVisual);
  366. FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  367. PngBitmapEncoder encoder = new PngBitmapEncoder();
  368. encoder.Interlace = PngInterlaceOption.On;
  369. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  370. encoder.Save(stream);
  371. stream.Close();
  372. }
  373. private ToolItem target;
  374. private void ListBoxShortCuts_DragOver(object sender, DragEventArgs e)
  375. {
  376. ListBoxEx MoreEx = GetListMoreControl();
  377. if (MoreEx == null) return;
  378. ListBoxEx ShortEx = GetListShortControl();
  379. if (ShortEx == null) return;
  380. var pos = e.GetPosition(ShortEx);
  381. Console.WriteLine($"pos:{pos.X} {pos.Y} this.Grid.ActualHeight1 {this.Grid.ActualHeight}");
  382. if (pos.Y < 0)
  383. {
  384. isMove = false;
  385. sourceImage.Visibility = Visibility.Hidden;
  386. sourceListItem = null;
  387. return;
  388. }
  389. if (pos.Y >= this.Grid.ActualHeight - 50)
  390. {
  391. isMove = false;
  392. sourceImage.Visibility = Visibility.Hidden;
  393. sourceListItem = null;
  394. return;
  395. }
  396. if (pos.X < 10)
  397. {
  398. isMove = false;
  399. sourceListItem = null;
  400. sourceImage.Visibility = Visibility.Hidden;
  401. return;
  402. }
  403. if (pos.X >= this.Grid.ActualWidth - 10)
  404. {
  405. isMove = false;
  406. sourceListItem = null;
  407. sourceImage.Visibility = Visibility.Hidden;
  408. return;
  409. }
  410. if (!isMoreCuts)
  411. {
  412. var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
  413. if (sourceAToolModule == null) return;
  414. int sourceIndex = quickTools.FindIndex(item => item.FnName == sourceAToolModule.FnName);
  415. //根据index找listbox对应的item
  416. sourceListItem = (ListItemQuickTool)(ShortEx.ItemContainerGenerator.ContainerFromIndex(sourceIndex) as FrameworkElement);
  417. if (sourceListItem == null)
  418. {
  419. return;
  420. }
  421. MoreEx.AllowDrop = false;
  422. sourceImage.Visibility = Visibility.Hidden;
  423. }
  424. else
  425. {
  426. var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
  427. if (sourceAToolModule == null) return;
  428. int sourceIndex = moreTools.FindIndex(item => item.FnName == sourceAToolModule.FnName);
  429. //根据index找listbox对应的item
  430. sourceListItem = (ListItemQuickTool)(MoreEx.ItemContainerGenerator.ContainerFromIndex(sourceIndex) as FrameworkElement);
  431. if (sourceListItem == null)
  432. {
  433. return;
  434. }
  435. isMove = true;
  436. //图片绑定
  437. ImageBinding(pos);
  438. }
  439. var result = VisualTreeHelper.HitTest(ShortEx, pos);
  440. if (result == null)
  441. {
  442. return;
  443. }
  444. //查找目标数据
  445. targetListItem = (ListItemQuickTool)CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  446. if (targetListItem == null)
  447. {
  448. //sourceImage.Visibility = Visibility.Hidden;
  449. return;
  450. }
  451. var targetAToolModule = targetListItem.Content as ToolItem;
  452. if (targetAToolModule == null) return;
  453. target = quickTools.Find(c => c.FnName == targetAToolModule.FnName);
  454. }
  455. /// <summary>
  456. ///图片绑定
  457. /// </summary>
  458. private void ImageBinding(System.Windows.Point pos)
  459. {
  460. sourceImage.Visibility = Visibility.Visible;
  461. sourceImage.Source = bitmapSource;
  462. sourceImage.Margin = new Thickness(pos.X - 80, pos.Y, 0, 0);
  463. moveImage.Source = sourceImage.Source;
  464. }
  465. private void ListBoxShortCuts_PreviewMouseMove(object sender, MouseEventArgs e)
  466. {
  467. if (e.LeftButton == MouseButtonState.Pressed)
  468. {
  469. ListBoxEx MoreEx = GetListMoreControl();
  470. if (MoreEx == null) return;
  471. ListBoxEx ShortEx = GetListShortControl();
  472. if (ShortEx == null) return;
  473. MoreEx.AllowDrop = false;
  474. var pos = e.GetPosition(ShortEx);
  475. HitTestResult result = VisualTreeHelper.HitTest(ShortEx, pos);
  476. if (result == null)
  477. {
  478. return;
  479. }
  480. quickSourceListItem = (ListItemQuickTool)CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  481. if (quickSourceListItem == null || quickSourceListItem.Content != ShortEx.SelectedItem)
  482. {
  483. return;
  484. }
  485. System.Windows.DataObject dataObj = new System.Windows.DataObject(quickSourceListItem.Content as ToolItem);
  486. if (dataObj != null)
  487. {
  488. isMoreCuts = false;
  489. DragDrop.DoDragDrop(ShortEx, dataObj, System.Windows.DragDropEffects.Move);
  490. }
  491. }
  492. else
  493. {
  494. sourceImage.Visibility = Visibility.Hidden;
  495. //moveImage.Visibility = Visibility.Hidden;
  496. }
  497. }
  498. private void sourceImage_Drop(object sender, DragEventArgs e)
  499. {
  500. ListBoxShortCuts_Drop(sender, e);
  501. }
  502. private void sourceImage_PreviewMouseMove(object sender, MouseEventArgs e)
  503. {
  504. ListBoxMoreCuts_PreviewMouseMove(sender, e);
  505. }
  506. private void sourceImage_DragOver(object sender, DragEventArgs e)
  507. {
  508. ListBoxShortCuts_DragOver(sender, e);
  509. }
  510. private void Label_PreviewMouseMove(object sender, MouseEventArgs e)
  511. {
  512. ListBoxMoreCuts_PreviewMouseMove(sender, e);
  513. }
  514. private void ListBoxMoreCuts_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  515. {
  516. //获取当前控件元素相对鼠标点击的位置(只有点击在当前A控件,会触发事件并返回相对A控件的鼠标位置)。
  517. Point pp = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法
  518. sourceImage.Visibility = Visibility.Hidden;
  519. if (pp.X < 0 || pp.Y < 0)
  520. {
  521. moveImage.Visibility = Visibility.Hidden;
  522. }
  523. }
  524. private void ListBoxShortCuts_SizeChanged(object sender, SizeChangedEventArgs e)
  525. {
  526. ListBoxEx ShortEx = GetListShortControl();
  527. if (ShortEx == null) return;
  528. ListBoxCutsWidthChanged(ShortEx);
  529. }
  530. private void ListBoxMoreCuts_SizeChanged(object sender, SizeChangedEventArgs e)
  531. {
  532. ListBoxEx MoreEx = GetListMoreControl();
  533. if (MoreEx == null) return;
  534. ListBoxCutsWidthChanged(MoreEx);
  535. }
  536. private void ListBoxCutsWidthChanged(ListBoxEx list)
  537. {
  538. if (list == null) return;
  539. //3x + 4y = containerWidth;
  540. //3x = y;
  541. //x为间距,y为item;
  542. double containerWidth = list.ActualWidth - 20;
  543. int widthItem = (int)containerWidth / 5;
  544. double margin = widthItem / 3;
  545. int i = 0;
  546. foreach (var item in list.Items)
  547. {
  548. i++;
  549. var listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
  550. if (listBoxItem != null)
  551. {
  552. if (i % 4 != 0)
  553. {
  554. listBoxItem.Margin = new Thickness(0, 0, margin, 0);
  555. }
  556. else
  557. {
  558. listBoxItem.Margin = new Thickness(0, 0, 0, 0);
  559. }
  560. listBoxItem.Width = widthItem;
  561. }
  562. }
  563. }
  564. private void BtnTools_Click(object sender, RoutedEventArgs e)
  565. {
  566. }
  567. private void BtnExptend_Click(object sender, RoutedEventArgs e)
  568. {
  569. UpdateExptend();
  570. }
  571. private void UpdateExptend()
  572. {
  573. //foreach (var item in allTools)
  574. //{
  575. // item.IsShowConciseContent = !(bool)BtnExptend.IsChecked;
  576. //}
  577. //foreach (var item in quickTools)
  578. //{
  579. // item.IsShowConciseContent = !(bool)BtnExptend.IsChecked;
  580. //}
  581. }
  582. //自适应流式布局,以防改需求备用
  583. //private void ListBoxMoreCutsWidthChanged()
  584. //{
  585. // double containerWidth = ListBoxShortCuts.ActualWidth;
  586. // int Rowcount = (int)containerWidth / 240;
  587. // double EndRow = containerWidth % 240.0;
  588. // Rowcount = EndRow >= 140 ? ++Rowcount : Rowcount;
  589. // int i = 0;
  590. // foreach (var item in ListBoxMoreCuts.Items)
  591. // {
  592. // i++;
  593. // var listBoxItem = ListBoxMoreCuts.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
  594. // if (listBoxItem != null)
  595. // {
  596. // if (i % Rowcount != 0)
  597. // {
  598. // listBoxItem.Margin = new Thickness(0, 0, 100, 0);
  599. // }
  600. // else
  601. // {
  602. // listBoxItem.Margin = new Thickness(0, 0, 0, 0);
  603. // }
  604. // }
  605. // }
  606. //}
  607. ///// <summary>
  608. ///// 扩展收缩UI
  609. ///// </summary>
  610. ///// <param name="IsShowConciseContent">是否收缩</param>
  611. //private void ShowToolsUI(ListBoxEx list,bool IsShowConciseContent)
  612. //{
  613. // foreach (var item in list.Items)
  614. // {
  615. // var listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
  616. // if (listBoxItem != null)
  617. // {
  618. // var viewItem = listBoxItem.ContentTemplate;
  619. // var myContentPresenter = CommonHelper.FindVisualChild<ContentPresenter>(listBoxItem);
  620. // var obj = viewItem.FindName("data", myContentPresenter);
  621. // var checkNum = obj as PDFToolItem;
  622. // if (checkNum != null)
  623. // {
  624. // checkNum.IsShowConciseContent = IsShowConciseContent;
  625. // }
  626. // }
  627. // }
  628. //}
  629. }
  630. }