在WPF中禁用时,按钮在文本框中的行为(视觉上)

时间:2011-11-30 12:26:21

标签: .net wpf button coding-style

我有一个带有一些背景颜色的按钮,但是当我将此按钮设置为禁用时,它看起来像一个空文本框。为什么会这样,可以解决。

1 个答案:

答案 0 :(得分:1)

您是否考虑过为按钮定义自己的模板。这是一个例子 请参阅“触发器属性= IsEnabled ...”部分。在这里,您可以根据需要为禁用按钮定义外观。我建议玩所有值并根据您的需要定义它。

<Style TargetType="Button">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="BorderBrush" Value="LightGray"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="bord" 
                     CornerRadius="5" 
                     BorderBrush="{TemplateBinding BorderBrush}"
                     BorderThickness="{TemplateBinding BorderThickness}"
                     TextElement.Foreground="{TemplateBinding Foreground}"
                     TextElement.FontWeight="{TemplateBinding FontWeight}">
                    <Border.Background>
                        <LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
                            <GradientStop Color="Gray" Offset="0"/>
                            <GradientStop Color="DarkGray" Offset="0.5"/>
                            <GradientStop Color="Black" Offset="0.5"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <ContentPresenter Content="{TemplateBinding Content}" 
                                 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" TargetName="bord">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
                                    <GradientStop Color="Gray" Offset="0"/>
                                    <GradientStop Color="DarkGray" Offset="0.5"/>
                                    <GradientStop Color="Black" Offset="0.5"/>
                                    <GradientStop Color="Black" Offset="0.7"/>
                                    <GradientStop Color="LightBlue" Offset="1"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="BorderThickness" TargetName="bord" Value="2"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" TargetName="bord" Value="Peru"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>