Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Button Background when is not enabled

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>
like image 754
Nick Avatar asked Jan 15 '13 12:01

Nick


People also ask

How do I change the background color of my clicking button?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste.

What is the color of disabled button?

How should disabled buttons appear when they're inactive? The way most designers indicate an inactive state is by graying out the button.

How can I change background color of clicking button in Android?

Inside the function use setBackgroundResource(R. color. button_color) function, this will set the background with color button_color.

How do I change the background of a button in Swift?

Add a button on your storyboard, select it Go to it's attribute inspector and select 'Background' property to choose the color.


1 Answers

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.

like image 128
Eirik Avatar answered Sep 28 '22 08:09

Eirik