简体   繁体   中英

Change button color when disabled

I use the following button and when isDisabled is true the button is transparent (here its always true for the example but in my program its bound to command...) what I want is that when the button is disabled the color remain the same just ligther, how can I achive that?

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

</Grid>

You have to edit or make a ControlTemplate for the Button. (use a key to bind the controltemplate to the button).

<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>

Control:

        <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}"  />
<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>

You can play with the colors .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM