Trigger to set wpf textbox borderbrush not working

我只是一个虾纸丫 提交于 2019-12-25 04:45:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!