QuickToolsContent.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 PDF_Master.Views.HomePanel.PDFTools;
  7. using PDFSettings;
  8. using System.Collections.Generic;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. namespace PDF_Master.Views.HomePanel.PDFTools
  13. {
  14. /// <summary>
  15. /// HomeToolIControl.xaml 的交互逻辑0
  16. ///
  17. /// </summary>
  18. public partial class QuickToolsContent : UserControl
  19. {
  20. PDFToolsHelper PDFtools;
  21. private QuickToolsContentViewModel ViewModel => DataContext as QuickToolsContentViewModel;
  22. public QuickToolsContent()
  23. {
  24. InitializeComponent();
  25. PDFtools = PDFToolsHelper.GetInstance();
  26. ListBoxToolBars.ItemsSource = PDFtools.QuickTools;
  27. ListBoxExpendToolBars.ItemsSource = PDFtools.QuickTools;
  28. }
  29. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  30. {
  31. if (ViewModel != null)
  32. {
  33. ViewModel.ExpendToolsHanlder -= ViewModel_ExpendToolsHanlder;
  34. ViewModel.ExpendToolsHanlder += ViewModel_ExpendToolsHanlder;
  35. }
  36. if (ListBoxToolBars.Items != null)
  37. {
  38. UpdateExptendUI(ViewModel.IsExpendTools);
  39. ListBoxToolBars.Items.Refresh();
  40. //方案调整,不需要缩小item宽度
  41. //ListBoxToolBarsWidthChanged();
  42. //ListBoxExpendToolBarsWidthChanged();
  43. }
  44. }
  45. private void ViewModel_ExpendToolsHanlder(object sender, bool e)
  46. {
  47. //UpdateToolBarsWidth();
  48. UpdateExptendUI(e);
  49. }
  50. private void UpdateExptendUI(bool e)
  51. {
  52. if (e)
  53. {
  54. GridAllTools.Height = 270;
  55. }
  56. else
  57. {
  58. GridAllTools.Height = 174;
  59. }
  60. }
  61. private void GridAllTools_SizeChanged(object sender, SizeChangedEventArgs e)
  62. {
  63. //方案调整,不需要缩小item宽度
  64. //UpdateToolBarsWidth();
  65. }
  66. private void UpdateToolBarsWidth()
  67. {
  68. ListBoxToolBarsWidthChanged();
  69. ListBoxExpendToolBarsWidthChanged();
  70. }
  71. private void ListBoxToolBarsWidthChanged()
  72. {
  73. if (ListBoxToolBars.Visibility == Visibility.Collapsed)
  74. return;
  75. double containerWidth = ListBoxToolBars.ActualWidth - 20;
  76. int widthItem = (int)containerWidth / 5;
  77. double margin = widthItem / 3;
  78. int i = 0;
  79. foreach (var item in ListBoxToolBars.Items)
  80. {
  81. i++;
  82. var listBoxItem = ListBoxToolBars.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
  83. if (listBoxItem != null)
  84. {
  85. if (i % 4 != 0)
  86. {
  87. listBoxItem.Margin = new Thickness(0, 0, margin, 0);
  88. }
  89. else
  90. {
  91. listBoxItem.Margin = new Thickness(0, 0, 0, 0);
  92. }
  93. listBoxItem.Width = widthItem;
  94. }
  95. }
  96. }
  97. private void ListBoxExpendToolBarsWidthChanged()
  98. {
  99. if (ListBoxExpendToolBars.Visibility == Visibility.Collapsed)
  100. return;
  101. double containerWidth = ListBoxExpendToolBars.ActualWidth - 20;
  102. int widthItem = (int)containerWidth / 5;
  103. double margin = widthItem / 3;
  104. int i = 0;
  105. foreach (var item in ListBoxExpendToolBars.Items)
  106. {
  107. i++;
  108. var listBoxItem = ListBoxExpendToolBars.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
  109. if (listBoxItem != null)
  110. {
  111. if (i % 4 != 0)
  112. {
  113. listBoxItem.Margin = new Thickness(0, 0, margin, 0);
  114. }
  115. else
  116. {
  117. listBoxItem.Margin = new Thickness(0, 0, 0, 0);
  118. }
  119. listBoxItem.Width = widthItem;
  120. }
  121. }
  122. }
  123. private void QuickTools_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
  124. {
  125. var toolItem = (sender as FrameworkElement).DataContext as ToolItem;
  126. var veiwModel = this.DataContext as QuickToolsContentViewModel;
  127. if (veiwModel != null && toolItem != null)
  128. {
  129. veiwModel.QuickToolsCommand.Execute(toolItem);
  130. }
  131. }
  132. }
  133. }