【问题标题】:How to override the enabled /disabled look on a button in wpf如何覆盖 wpf 中按钮的启用/禁用外观
【发布时间】:2015-02-23 21:12:46
【问题描述】:

我假设这可能是一个风格问题,但作为 wpf 的新手,我不确定我应该改变什么。我已经构建了一个包含各种按钮的用户控件(我将其背景颜色设置为透明)。尽管如此,当添加到表单或其他用户控件时,它们会在禁用时显示彩色背景,或者在启用时被鼠标滑过(见下图)

我需要做些什么来删除按钮禁用状态下的灰色背景,以及启用时在翻转时出现的蓝色背景。我的目标是尝试保持按钮图像的清晰再现。

谢谢

【问题讨论】:

  • 目前我使用按钮的 IsEnabledChanged 事件来交换显示的图像,所以如果他们看到按钮图像的灰色版本,它就会被禁用。

标签: wpf user-controls


【解决方案1】:

您需要使用Style. 覆盖Button 控件的ControlTemplate 以下Style 将适用于所有Button 控件,但您可以为Style 指定Key如果您愿意,可以在特定的Button 控件上使用它。

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="ButtonContent">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="ButtonContent"
                                    Property="Background"
                                    Value="Transparent"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

【讨论】:

    猜你喜欢
    • 2011-12-16
    • 2021-05-18
    • 2021-12-31
    • 1970-01-01
    • 2011-01-07
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多