From faec7df0dd0712cf8630a1495fa48c770d210211 Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Wed, 30 Jul 2025 17:45:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ComboBox=20=E7=9A=84=E5=85=89=E6=A0=87?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E4=B8=8D=E4=BC=9A=E9=9A=8F=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E8=89=B2=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Res/Styles/ComboBox.xaml | 4 ++- src/MaaWpfGui/Res/Styles/ComboBox.xaml.cs | 44 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/MaaWpfGui/Res/Styles/ComboBox.xaml.cs diff --git a/src/MaaWpfGui/Res/Styles/ComboBox.xaml b/src/MaaWpfGui/Res/Styles/ComboBox.xaml index a50d3d187e..2173654cb2 100644 --- a/src/MaaWpfGui/Res/Styles/ComboBox.xaml +++ b/src/MaaWpfGui/Res/Styles/ComboBox.xaml @@ -1,12 +1,14 @@ - + diff --git a/src/MaaWpfGui/Res/Styles/ComboBox.xaml.cs b/src/MaaWpfGui/Res/Styles/ComboBox.xaml.cs new file mode 100644 index 0000000000..d312ec1d9e --- /dev/null +++ b/src/MaaWpfGui/Res/Styles/ComboBox.xaml.cs @@ -0,0 +1,44 @@ +using System.ComponentModel; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using JetBrains.Annotations; + +namespace MaaWpfGui.Res.Styles +{ + /// + /// ComboBox 的光标颜色不会跟随主题变化 + /// + [UsedImplicitly] + public partial class ComboBox + { + private void ComboBox_Loaded_SetCaretBrush(object sender, RoutedEventArgs e) + { + if (sender is not System.Windows.Controls.ComboBox cb) + { + return; + } + + SetCaret(); + + // 订阅 IsEditable 变化 + var dpd = DependencyPropertyDescriptor.FromProperty(System.Windows.Controls.ComboBox.IsEditableProperty, typeof(System.Windows.Controls.ComboBox)); + dpd.AddValueChanged(cb, (_, _) => SetCaret()); + return; + + void SetCaret() + { + if (!cb.IsEditable) + { + return; + } + + cb.ApplyTemplate(); + if (cb.Template.FindName("PART_EditableTextBox", cb) is TextBox tb) + { + tb.CaretBrush = (Brush)Application.Current.Resources["PrimaryTextBrush"]; + } + } + } + } +}