| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 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
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- 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);
- }
- }
- }
|