Change Button Background when is not enabled
Asked Answered
L

2

11

I need to change my Button background (as SolidColorBrush for example) only when it is not enabled (IsEnabled == false).
How can I do?

Have I to modify the Button Style using the XAML or can I do this work programmatically? What is the correct XAML code, to change only the Background when it is not enabled?

I tried the following XAML code but it has no effect:

<Button>
<Button.Style>
    <Style TargetType="Button">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="Red"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>
</Button>
Lianne answered 15/1, 2013 at 12:20 Comment(2)
check this article: infosysblogs.com/microsoft/2010/07/…Impostor
why doesn't this work? has anyone got explaination?Garrote
E
7

You can change the background by editing the template. You'll find the default template for Button here.

In the trigger for IsEnabled you can simply add something like this:

<Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>

EDIT: Try this instead then;

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="Overlay" CornerRadius="2">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Overlay" Property="Background" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<StackPanel>
    <Button Content="Button" IsEnabled="False"/>
</StackPanel>

Just change it to suit your needs.

Ensiform answered 15/1, 2013 at 12:37 Comment(4)
It is not the default template. Where can I find the correct template?Lianne
What version are you looking for? You can change the version in the top there for other versions.Ensiform
I'm looking for de default version. I want to change only the background when my button is disabled. The link that you provided me has a style that changes my button default style. Try yourself..Lianne
I meant .NET version. (ie. 4.0 - msdn.microsoft.com/en-us/library/ms753328%28v=vs.100%29.aspx, ). Try the code I just added instead then, it should work.Ensiform
K
-1

You can use a Style trigger:

<Image.Style>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsEnabled}" Value="False">
                <Setter Property="Image.Source" Value="pack://application:,,,/disabled.png" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Image.Style>
Kalikow answered 15/1, 2013 at 12:30 Comment(2)
Why Image.Style? I need to change the SolidColorBrush of my Button.Lianne
Use Button.Background instead of Image.Source and set color (or specific brush) in ValueDropforge

© 2022 - 2025 — McMap. All rights reserved.