MainWindow.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace WpfTest
  17. {
  18. /// <summary>
  19. /// MainWindow.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. string patch = $"{AppDomain.CurrentDomain.BaseDirectory}report_with_images.pdf";
  27. LoadPdf(patch);
  28. }
  29. private void Window_Loaded(object sender, RoutedEventArgs e)
  30. {
  31. testcombo.Items.Add("1");
  32. testcombo.Items.Add("2");
  33. }
  34. private void testcombo_Loaded(object sender, RoutedEventArgs e)
  35. {
  36. var textbox = (TextBox)testcombo.Template.FindName("PART_EditableTextBox", testcombo);
  37. if (textbox != null)
  38. {
  39. var parent = (Border)textbox.Parent;
  40. parent.Background = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF0087FF"));
  41. }
  42. }
  43. private async void LoadPdf(string pdfFilePath)
  44. {
  45. // 加载 PDF 文件
  46. await PdfWebView.EnsureCoreWebView2Async(null);
  47. PdfWebView.CoreWebView2.Navigate(pdfFilePath);
  48. }
  49. PopupTest popup = new PopupTest();
  50. private void Button_Click(object sender, RoutedEventArgs e)
  51. {
  52. //myPopup.IsOpen = !myPopup.IsOpen;
  53. if(popup.IsLoaded)
  54. {
  55. popup.Close();
  56. }
  57. else
  58. {
  59. popup.Show();
  60. }
  61. }
  62. }
  63. public static class ComboBoxHelper
  64. {
  65. public static readonly DependencyProperty EditBackgroundProperty = DependencyProperty.RegisterAttached(
  66. "EditBackground", typeof(Brush), typeof(ComboBoxHelper), new PropertyMetadata(default(Brush), EditBackgroundChanged));
  67. // 新增:下拉框背景属性
  68. public static readonly DependencyProperty DropdownBackgroundProperty = DependencyProperty.RegisterAttached(
  69. "DropdownBackground", typeof(Brush), typeof(ComboBoxHelper), new PropertyMetadata(default(Brush), DropdownBackgroundChanged));
  70. private static void EditBackgroundChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
  71. {
  72. var combo = dependencyObject as ComboBox;
  73. if (combo != null)
  74. {
  75. if (!combo.IsLoaded)
  76. {
  77. RoutedEventHandler comboOnLoaded = null;
  78. comboOnLoaded = delegate (object sender, RoutedEventArgs eventArgs)
  79. {
  80. EditBackgroundChanged(dependencyObject, args);
  81. combo.Loaded -= comboOnLoaded;
  82. };
  83. combo.Loaded += comboOnLoaded;
  84. return;
  85. }
  86. var part = combo.Template.FindName("PART_EditableTextBox", combo);
  87. var tb = part as TextBox;
  88. if (tb != null)
  89. {
  90. var parent = tb.Parent as Border;
  91. if (parent != null)
  92. {
  93. parent.Background = (Brush)args.NewValue;
  94. }
  95. }
  96. }
  97. }
  98. // 新增:下拉框背景改变处理方法
  99. private static void DropdownBackgroundChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
  100. {
  101. var combo = dependencyObject as ComboBox;
  102. if (combo != null)
  103. {
  104. if (!combo.IsLoaded)
  105. {
  106. RoutedEventHandler comboOnLoaded = null;
  107. comboOnLoaded = delegate (object sender, RoutedEventArgs eventArgs)
  108. {
  109. DropdownBackgroundChanged(dependencyObject, args);
  110. combo.Loaded -= comboOnLoaded;
  111. };
  112. combo.Loaded += comboOnLoaded;
  113. return;
  114. }
  115. // 查找下拉框的背景元素
  116. combo.ApplyTemplate();
  117. var dropdownBorder = combo.Template.FindName("DropDownBorder", combo) as Border;
  118. if (dropdownBorder != null)
  119. {
  120. dropdownBorder.Background = (Brush)args.NewValue;
  121. }
  122. }
  123. }
  124. [AttachedPropertyBrowsableForType(typeof(ComboBox))]
  125. public static void SetEditBackground(DependencyObject element, Brush value)
  126. {
  127. element.SetValue(EditBackgroundProperty, value);
  128. }
  129. [AttachedPropertyBrowsableForType(typeof(ComboBox))]
  130. public static Brush GetEditBackground(DependencyObject element)
  131. {
  132. return (Brush)element.GetValue(EditBackgroundProperty);
  133. }
  134. // 新增:下拉框背景的访问器方法
  135. [AttachedPropertyBrowsableForType(typeof(ComboBox))]
  136. public static void SetDropdownBackground(DependencyObject element, Brush value)
  137. {
  138. element.SetValue(DropdownBackgroundProperty, value);
  139. }
  140. [AttachedPropertyBrowsableForType(typeof(ComboBox))]
  141. public static Brush GetDropdownBackground(DependencyObject element)
  142. {
  143. return (Brush)element.GetValue(DropdownBackgroundProperty);
  144. }
  145. }
  146. }