当 WPF TextBox 获得焦点时,如何轻松更改它的样式? [英] How can I easily change the style of WPF TextBox when it gets focused?

查看:126
本文介绍了当 WPF TextBox 获得焦点时,如何轻松更改它的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WPF 应用程序中有一个文本框,背景颜色为蓝色".当它获得焦点时,背景颜色默认变为白色".当 TextBox 获得焦点时,我希望背景颜色具有另一种颜色(比如 DodgerBlue").

I have a TextBox in my WPF app, with background color "Blue". When it receives focus, the background color changes to "White" by default. I want the background color to have another color when the TextBox gets focused (say "DodgerBlue").

我在网络上所能找到的都是令人惊奇的样式或模板示例,它们定义了 TextBox 的所有可能的 VisualState.

All I can find in the web are amazingly examples of styles or templates, defining all possible VisualStates of the TextBox.

是否无法创建一个仅针对特定情况(即 TextBox 具有焦点时)的简短模板?

Is it not possible to create a short template targeting only that specific situation (i.e. when the TextBox has focus)?

谢谢.

推荐答案

您可以使用简单的样式触发器:

You can use a simple style trigger:

<TextBox>
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Background" Value="Tomato" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

应该可以...

这篇关于当 WPF TextBox 获得焦点时,如何轻松更改它的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆