问题描述
当文本框具有焦点时,我试图在简单的文本框上设置背景边框.
Im trying to set the background borderbrush on a simple textbox when the textbox has focus.
这就是我的风格
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsFocused" Value="False">
<Setter Property="BorderBrush" Value="Blue"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="Red"/>
<!--<Setter Property="Background" Value="Red"/>-->
</Trigger>
IsFocus = False 可以正常工作,但是 true 的触发器却没有.
IsFocus = False works correctly however the trigger for true doesnt.
我已经注释掉了背景设置器,但如果我取消注释它,我可以看到背景设置为红色.
I have commented out the background setter but if I uncomment it I can see that the background is been set to red.
当文本框有焦点时,我需要做什么不同的事情来改变边框颜色?
What do I need to do differently to change the border colour when the textbox has focus?
谢谢.
推荐答案
如果你的 BorderThickness 为1"(默认)那么你得到的重点样式就是标准的 3D 控件之一.您可以使用 contenttemplate 重新设置控件的样式,但一个简单的(虽然不是最优雅的解决方案)是简单地将边框厚度设置为1"以外的值,就像我在下面的示例中所做的那样.
If your BorderThickness to "1" (default) then the focussed style you get is the standard 3D one of the control. You could restyle the control using a contenttemplate but a simple (although not the most elegant solution) would be to simply set the border thickness to something other than "1" as i've done in the sample below.
<TextBox Width="200" Height="25" BorderThickness="0.99">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Red" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="False">
<Setter Property="BorderBrush" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
这篇关于触发设置 wpf 文本框边框不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!