WPF 文本框边框何时被选中?

发布于 2024-09-25 22:06:02 字数 1063 浏览 1 评论 0原文

我想让 WPF 文本框具有深蓝色边框且厚度等于 1。即使选择了文本框,我也想让 WPF 具有此边框(深蓝色,厚度设置为 1)。

我尝试通过以下代码完成此任务。然而,它根本不起作用。 有什么想法或提示吗?任何帮助将不胜感激。

  <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"/>
            </Trigger>
            <Trigger Property="TextBox.IsMouseOver"    Value="False">
                <Setter Property="BorderBrush" Value="DarkBlue"/>
                <Setter Property="BorderThickness" Value="1"/>
            </Trigger>
        </Style.Triggers>


    </Style>

PS 请注意,文本框没有 IsSelected 属性。

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"/>
            </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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耶耶耶 2024-10-02 22:06:02

看看这是你想要的...

<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> 

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> 
止于盛夏 2024-10-02 22:06:02

我认为您的问题是由于触发器属性值包含TextBox。您只需要该房产的名称。

    <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>

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>
甜尕妞 2024-10-02 22:06:02

检查 FrameworkElement 对象(祖先的文本框)。其目的是定义选择元素时应用的样式。

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

奶茶白久 2024-10-02 22:06:02

当“IsMouseOver”为 True 和 False 时,您具有相同的逻辑。换一个,你应该会看到一些东西。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文