using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfTest { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); string patch = $"{AppDomain.CurrentDomain.BaseDirectory}report_with_images.pdf"; LoadPdf(patch); } private void Window_Loaded(object sender, RoutedEventArgs e) { testcombo.Items.Add("1"); testcombo.Items.Add("2"); } private void testcombo_Loaded(object sender, RoutedEventArgs e) { var textbox = (TextBox)testcombo.Template.FindName("PART_EditableTextBox", testcombo); if (textbox != null) { var parent = (Border)textbox.Parent; parent.Background = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF0087FF")); } } private async void LoadPdf(string pdfFilePath) { // 加载 PDF 文件 await PdfWebView.EnsureCoreWebView2Async(null); PdfWebView.CoreWebView2.Navigate(pdfFilePath); } PopupTest popup = new PopupTest(); private void Button_Click(object sender, RoutedEventArgs e) { //myPopup.IsOpen = !myPopup.IsOpen; if(popup.IsLoaded) { popup.Close(); } else { popup.Show(); } } } public static class ComboBoxHelper { public static readonly DependencyProperty EditBackgroundProperty = DependencyProperty.RegisterAttached( "EditBackground", typeof(Brush), typeof(ComboBoxHelper), new PropertyMetadata(default(Brush), EditBackgroundChanged)); // 新增:下拉框背景属性 public static readonly DependencyProperty DropdownBackgroundProperty = DependencyProperty.RegisterAttached( "DropdownBackground", typeof(Brush), typeof(ComboBoxHelper), new PropertyMetadata(default(Brush), DropdownBackgroundChanged)); private static void EditBackgroundChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { var combo = dependencyObject as ComboBox; if (combo != null) { if (!combo.IsLoaded) { RoutedEventHandler comboOnLoaded = null; comboOnLoaded = delegate (object sender, RoutedEventArgs eventArgs) { EditBackgroundChanged(dependencyObject, args); combo.Loaded -= comboOnLoaded; }; combo.Loaded += comboOnLoaded; return; } var part = combo.Template.FindName("PART_EditableTextBox", combo); var tb = part as TextBox; if (tb != null) { var parent = tb.Parent as Border; if (parent != null) { parent.Background = (Brush)args.NewValue; } } } } // 新增:下拉框背景改变处理方法 private static void DropdownBackgroundChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { var combo = dependencyObject as ComboBox; if (combo != null) { if (!combo.IsLoaded) { RoutedEventHandler comboOnLoaded = null; comboOnLoaded = delegate (object sender, RoutedEventArgs eventArgs) { DropdownBackgroundChanged(dependencyObject, args); combo.Loaded -= comboOnLoaded; }; combo.Loaded += comboOnLoaded; return; } // 查找下拉框的背景元素 combo.ApplyTemplate(); var dropdownBorder = combo.Template.FindName("DropDownBorder", combo) as Border; if (dropdownBorder != null) { dropdownBorder.Background = (Brush)args.NewValue; } } } [AttachedPropertyBrowsableForType(typeof(ComboBox))] public static void SetEditBackground(DependencyObject element, Brush value) { element.SetValue(EditBackgroundProperty, value); } [AttachedPropertyBrowsableForType(typeof(ComboBox))] public static Brush GetEditBackground(DependencyObject element) { return (Brush)element.GetValue(EditBackgroundProperty); } // 新增:下拉框背景的访问器方法 [AttachedPropertyBrowsableForType(typeof(ComboBox))] public static void SetDropdownBackground(DependencyObject element, Brush value) { element.SetValue(DropdownBackgroundProperty, value); } [AttachedPropertyBrowsableForType(typeof(ComboBox))] public static Brush GetDropdownBackground(DependencyObject element) { return (Brush)element.GetValue(DropdownBackgroundProperty); } } }