gpt4 book ai didi

c# - 在 GotFocus() 时更改 Wpf 文本框的聚焦边框颜色

转载 作者:太空狗 更新时间:2023-10-29 22:09:04 24 4
gpt4 key购买 nike

我想要的:当任何文本框获得焦点时,将边框颜色更改为黄色。

我尝试过的:

<Window.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="Yellow"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>

没有快乐。无法弄清楚为什么边框仍然是蓝色的。这与 How to change the color of the Border of a TextBox when it has focus? 相似,但不是重复的.

最佳答案

需要修改TextBox的控件模板。向样式添加触发器是不够的。这应该有效:

<Style TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="Yellow"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

您可以在WPF 源代码中找到原始样式模板:https://github.com/dotnet/wpf/blob/c271205b80c27df976acbd7236ec637090d127c1/src/Microsoft.DotNet.Wpf/src/Themes/XAML/TextBox.xaml#L415-L441

关于c# - 在 GotFocus() 时更改 Wpf 文本框的聚焦边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42013899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com