Change button Background on IsEnabled using expression blend

144 views Asked by At

I want to change the Background color of a Button and the Fill color of a Path in WPF. The color values should update based on the current state of the Button's IsEnabled property.

(I want to create a dark grey Background color if the button is disabled and update the Path's Fill color to be light gray if the button is disabled, if the button is enabled the Background and Fill colors should be what they currently are)

I know this could be done with some converters....but could I do this more easily only by using Blend (states and triggers)? If so please let me know how?

1

There are 1 answers

1
Sats On BEST ANSWER

I have overridden the default style and changed the background color to DarkGray has you have asked using Triggers. Rest I have kept the same button style using the TemplateBinding

<Button x:Name="Button1" Content="Login" HorizontalAlignment="Left"  Height="31"  Margin="10,41,0,-41" Grid.Row="1"  VerticalAlignment="Top" Width="129" Click="Button_Click_1">
        <Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="OverridesDefaultStyle" Value="True" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type Button}">
                                    <Grid Background="{TemplateBinding Background}">
                                        <ContentPresenter x:Name="MyContentPresenter" 
                                                          Content="{TemplateBinding Content}"
                                                          HorizontalAlignment="Center" 
                                                          VerticalAlignment="Center" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>

                        <Setter Property="Background" Value="DarkGray"></Setter>
                </Trigger>
                <Trigger Property="IsEnabled" Value="True">
                        <Setter Property="Background" Value="#FFDDDDDD"></Setter>
                </Trigger>

            </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>