禁用时的 WPF 文本框样式

标签 wpf

我正在尝试创建一个简单的文本框样式。我想创建一个触发器来在禁用文本框时更改颜色:

<Style TargetType="{x:Type TextBox}">

    <Setter Property="Background" Value="White"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="FontSize" Value="{StaticResource NormalFontSize}"/>
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="FontStyle" Value="Normal"/>
    <Setter Property="FontFamily" Value="{StaticResource FontFamilyName}"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="TextAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="{StaticResource listItemHighlightBackground}"/>
                        <Setter Property="Foreground" Value="{StaticResource disabledArrowBackground}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>

当我使用触发器时,文本框消失了

这里有什么问题吗?

谢谢

最佳答案

ControlTemplate 替换该控件类型的模板。看起来你误解了它的用法。在触发器之前,尝试添加一些要显示的可视对象,例如边框,它就会出现。更好的是,根本不要使用 ControlTemplate ...您只需要在样式上设置一个属性触发器,例如:

<Style.Triggers>
    <Trigger Property="IsEnabled" Value="false">
        <Setter Property = "Foreground" Value="Green"/>
    </Trigger>
</Style.Triggers>

关于禁用时的 WPF 文本框样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30488656/

相关文章:

c# - 如何以只读模式打开Excel文件?

wpf - 删除右侧的空白处/列

c# - MVVM DynamicObject + Entity Framework STE 实体 + 属性更改行为

c# - call property change 当它的属性更新时回调

.net - 如何使用 WixToolset v3.11 修改 json

c# - 数据模型中没有为 TextBox Text 属性调用 Setter

c# - 在 WPF 的代码隐藏中为 ListBox 创建 ItemTemplate

c# - XML序列化——序列化用户控件中的属性

wpf - WPF4 中的 Microsoft 图表控件

c# - 我应该在 C# 中为我的项目使用 WPF 还是 Windows 窗体应用程序?