禁用时更改按钮颜色

时间:2014-03-03 15:17:21

标签: c# wpf xaml mvvm

我使用以下按钮,当isDisabled为true时,按钮是透明的 (这里的例子总是如此,但在我的程序中它的命令...) 我想要的是,当按钮被禁用时,颜色保持不变只是ligther, 我该如何实现呢?

    <Button  
                             Width="100"
                             Height="25"
                             Background="#f0ab00"
                             Content="Run"
                             FontSize="16"
                             Foreground="#ffffff"
                             IsDefault="True"
                             Margin="10,0,20,0"
                             IsEnabled="false" />

</Grid>

2 个答案:

答案 0 :(得分:5)

您必须为Button编辑或制作ControlTemplate。 (使用键将controltemplate绑定到按钮)。

<Window.Resources>
    <ControlTemplate x:Key="ButtonBaseControlTemplate1" TargetType="{x:Type ButtonBase}">
        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
            <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="Button.IsDefaulted" Value="True">
                <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" TargetName="border" Value="#FFBEE6FD"/>
                <Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/>
                <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
            </Trigger>
            <Trigger Property="ToggleButton.IsChecked" Value="True">
                <Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/>
                <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" TargetName="border" Value="Red"/>
                <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>

控制:

        <Button  
                         Width="100"
                         Height="25"
                         Background="#f0ab00"
                         Content="Run"
                         FontSize="16"
                         Foreground="#ffffff"

                         IsDefault="True"
                         Margin="10,0,20,0"
                         IsEnabled="false" Template="{DynamicResource ButtonBaseControlTemplate1}"  />

答案 1 :(得分:3)

<Style TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="#ffffff"/>
    <Setter Property="Background" Value="#f0ab00"/>        
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Background" Value="#f0aa00"/>
        </Trigger>
    </Style.Triggers>
</Style>

你可以玩颜色。