WPF - Default Button Disable Style When Foreground applied

Viewed 20042

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

5 Answers

The following code is working for me:

<ControlTemplate TargetType="{x:Type ToggleButton}">
     <Grid x:Name="grid" Background="{StaticResource FlatButtonNavigation}">
          <Border Name="ButtonBorder" CornerRadius="5,5,5,5"  
               BorderBrush="Gray" BorderThickness="2">
               <ContentPresenter Name="MyContentPresenter"
                   HorizontalAlignment="center"
                   RecognizesAccessKey="True"
                   SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                   TextBlock.FontSize="{TemplateBinding FontSize}"
                   TextBlock.Foreground="{StaticResource LeftPanelFontColor}"
                   Margin="10"/>
           </Border>
        </Grid>
        <ControlTemplate.Triggers>
           <Trigger Property="IsEnabled" Value="False">
              <Setter TargetName="ButtonBorder" Property="Background"  Value="LightGray"/>
              <Setter TargetName="ButtonBorder" Property="BorderBrush"  Value="#FFADB2B5"/>
              <Setter TargetName="MyContentPresenter" Property="TextBlock.Foreground" Value="White"></Setter>
           </Trigger>
     </ControlTemplate.Triggers>
</ControlTemplate>

Can be worked using styles as mentioned in the other answers, but there is no need to go outside the button to define the style, you can define a style explicitly related to the particular button:

<Button Width="150" Height="50" Content="Test">
   <Button.Style>
      <Style TargetType="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>
      </Button.Style>
   </Button>
</StackPanel>
Related