问题
Im trying to set the background borderbrush on a simple textbox when the textbox has focus.
This is my style
<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 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?
Thank you.
回答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>
来源:https://stackoverflow.com/questions/6116675/trigger-to-set-wpf-textbox-borderbrush-not-working