QuickToolsContent.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using PDF_Office.CustomControl;
  2. using PDF_Office.Helper;
  3. using PDF_Office.Model.PDFTool;
  4. using PDF_Office.ViewModels.HomePanel;
  5. using PDF_Office.ViewModels.HomePanel.PDFTools;
  6. using PDF_Office.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_Office.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. ListBoxToolBarsWidthChanged();
  41. ListBoxExpendToolBarsWidthChanged();
  42. }
  43. }
  44. private void ViewModel_ExpendToolsHanlder(object sender, bool e)
  45. {
  46. UpdateToolBarsWidth();
  47. UpdateExptendUI(e);
  48. }
  49. private void UpdateExptendUI(bool e)
  50. {
  51. if (e)
  52. {
  53. GridAllTools.Height = 270;
  54. }
  55. else
  56. {
  57. GridAllTools.Height = 174;
  58. }
  59. }
  60. private void GridAllTools_SizeChanged(object sender, SizeChangedEventArgs e)
  61. {
  62. UpdateToolBarsWidth();
  63. }
  64. private void UpdateToolBarsWidth()
  65. {
  66. ListBoxToolBarsWidthChanged();
  67. ListBoxExpendToolBarsWidthChanged();
  68. }
  69. private void ListBoxToolBarsWidthChanged()
  70. {
  71. if (ListBoxToolBars.Visibility == Visibility.Collapsed)
  72. return;
  73. double containerWidth = ListBoxToolBars.ActualWidth - 20;
  74. int widthItem = (int)containerWidth / 5;
  75. double margin = widthItem / 3;
  76. int i = 0;
  77. foreach (var item in ListBoxToolBars.Items)
  78. {
  79. i++;
  80. var listBoxItem = ListBoxToolBars.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
  81. if (listBoxItem != null)
  82. {
  83. if (i % 4 != 0)
  84. {
  85. listBoxItem.Margin = new Thickness(0, 0, margin, 0);
  86. }
  87. else
  88. {
  89. listBoxItem.Margin = new Thickness(0, 0, 0, 0);
  90. }
  91. listBoxItem.Width = widthItem;
  92. }
  93. }
  94. }
  95. private void ListBoxExpendToolBarsWidthChanged()
  96. {
  97. if (ListBoxExpendToolBars.Visibility == Visibility.Collapsed)
  98. return;
  99. double containerWidth = ListBoxExpendToolBars.ActualWidth - 20;
  100. int widthItem = (int)containerWidth / 5;
  101. double margin = widthItem / 3;
  102. int i = 0;
  103. foreach (var item in ListBoxExpendToolBars.Items)
  104. {
  105. i++;
  106. var listBoxItem = ListBoxExpendToolBars.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
  107. if (listBoxItem != null)
  108. {
  109. if (i % 4 != 0)
  110. {
  111. listBoxItem.Margin = new Thickness(0, 0, margin, 0);
  112. }
  113. else
  114. {
  115. listBoxItem.Margin = new Thickness(0, 0, 0, 0);
  116. }
  117. listBoxItem.Width = widthItem;
  118. }
  119. }
  120. }
  121. private void QuickTools_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
  122. {
  123. var toolItem = (sender as FrameworkElement).DataContext as ToolItem;
  124. var veiwModel = this.DataContext as QuickToolsContentViewModel;
  125. if (veiwModel != null && toolItem != null)
  126. {
  127. veiwModel.QuickToolsCommand.Execute(toolItem);
  128. }
  129. }
  130. }
  131. }