开发者

WPF TextBox Border when selected?

开发者 https://www.devze.com 2023-01-18 10:39 出处:网络
I want to make a WPF TextBox have a DarkBlue border and thickness equal to 1. I want to make the WPF have this border ( DarkBlue, thickness set to 1 ) even when the TextBox is selected.

I want to make a WPF TextBox have a DarkBlue border and thickness equal to 1. I want to make the WPF have this border ( DarkBlue, thickness set to 1 ) even when the TextBox is selected.

I tried doing this task by the following code. However, it doesn't work at all. Any ideas or hints ? Any help would be greatly appreciated.

  <Style x:Key="ReadOnlyLargeTextBox" TargetType="{x:Type TextBox}" >
        <Setter Property="Height" Value="80"/>
        <Setter Property="MaxHeight" Value="80"/>

        <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
        <Style.Triggers>
            <Trigger Property="TextBox.IsMouseOver"    Value="True">
                <Setter Property="BorderBrush" Value="DarkBlue"/>
                <Setter Property="BorderThickness" Value="1"/>
           开发者_如何学C </Trigger>
            <Trigger Property="TextBox.IsMouseOver"    Value="False">
                <Setter Property="BorderBrush" Value="DarkBlue"/>
                <Setter Property="BorderThickness" Value="1"/>
            </Trigger>
        </Style.Triggers>


    </Style>

P.S Note that the text box does not have an IsSelected property.


just see is this you want...

<Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                <Border x:Name="bg" BorderBrush="#FF825E5E" BorderThickness="1">
                        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
                            <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
                            <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style> 


I think your problem is due to having the Trigger Property value containing TextBox. You just need the name of the property.

    <Style x:Key="ReadOnlyLargeTextBox" TargetType="{x:Type TextBox}">
        <Setter Property="Height" Value="80"/>
        <Setter Property="MaxHeight" Value="80"/>

        <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>

        <Style.Triggers>
            <Trigger Property="IsFocused"  Value="True">
                <Setter Property="BorderBrush" Value="Blue"/>
                <Setter Property="BorderThickness" Value="1"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="BorderBrush" Value="Blue"/>
                <Setter Property="BorderThickness" Value="1"/>
            </Trigger>
        </Style.Triggers>
    </Style>


Check FocusVisualStyle property of the FrameworkElement object (ancestor of TextBox). It's purpose is to define style applied when an element is selected.


You have the same logic for when "IsMouseOver" True as well False. Change one and you should see something.

0

精彩评论

暂无评论...
验证码 换一张
取 消