Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Default Button Disable Style When Foreground applied

Tags:

wpf

Button is applied with a Foreground when it is enabled. If it is Set as Disabled, default Button's Disabled Foreground need to apply.

<Button Width="150"
        Height="50"
        Content="Test"
        Foreground="Red"
        IsEnabled="False" />

I have Triggers for this button like

<Trigger Property="IsEnabled" Value="false">
    <Setter Property="Foreground" Value="#FFADADAD"/>
</Trigger>

This Foreground is not applied when it is enabled.

Any idea on this?

Need like below,

enter image description here

enter image description here

like image 634
Sankarann Avatar asked Dec 27 '13 07:12

Sankarann


1 Answers

If you set the properties locally the trigger will not be able to change the value due to precedence.

Move the Foreground and IsEnabled property into the style:

<StackPanel>
<StackPanel.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="IsEnabled" Value="True"/>
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="#FFADADAD"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</StackPanel.Resources>
<Button Width="150" 
    Height="50"
    Content="Test">
</Button>

like image 146
Mohd Ahmed Avatar answered Sep 18 '22 08:09

Mohd Ahmed