fix: ComboBox 的光标颜色不会随主题色变化

This commit is contained in:
uye
2025-07-30 17:45:16 +08:00
parent cfd4341390
commit faec7df0dd
2 changed files with 47 additions and 1 deletions

View File

@@ -1,12 +1,14 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:uiBehaviors="clr-namespace:MaaWpfGui.Extensions.UIBehaviors">
<ResourceDictionary x:Class="MaaWpfGui.Res.Styles.ComboBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:uiBehaviors="clr-namespace:MaaWpfGui.Extensions.UIBehaviors">
<Style BasedOn="{StaticResource ComboBoxExtend}" TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="{DynamicResource RegionBrushOpacity25}" />
<Setter Property="uiBehaviors:ClipboardInterceptor.EnableSafeClipboard" Value="True" />
<EventSetter Event="Loaded" Handler="ComboBox_Loaded_SetCaretBrush" />
</Style>
<Style BasedOn="{StaticResource ComboBoxExtend}" TargetType="{x:Type hc:ComboBox}">
<Setter Property="Background" Value="{DynamicResource RegionBrushOpacity25}" />
<Setter Property="uiBehaviors:ClipboardInterceptor.EnableSafeClipboard" Value="True" />
<Setter Property="CaretBrush" Value="{DynamicResource PrimaryTextBrush}" />
</Style>
</ResourceDictionary>

View File

@@ -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
{
/// <summary>
/// ComboBox 的光标颜色不会跟随主题变化
/// </summary>
[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"];
}
}
}
}
}