Matt
Matt

Reputation: 7254

WPF, how to make button background blink upon click, using xaml

I want all buttons to blink for 5 seconds when they are clicked.

I have the following code so far. Can someone please help me in the right direction?

Edit

If animating the button background is tougher to animate then I can easily settle with the foreground animation.

<Style TargetType="{x:Type Button}">
        <Style.Triggers>
            <EventTrigger RoutedEvent="Click">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard BeginTime="00:00:00" 
                                RepeatBehavior="Forever" 
                                Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)">
                            <ColorAnimation From="Black" To="Red" Duration="0:0:5"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>

Upvotes: 0

Views: 9859

Answers (3)

Jeroen Doppenberg
Jeroen Doppenberg

Reputation: 1558

1.Yes the style is applied to all buttons

2.Animate backgrouund Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)". Make sure your animation style is not overwritten by the default styles. ref stackoverflow

3.Maybe run 2 opacity animations at the same time. One going from transparant to black and the other from red to transparant at the same time.This is just an idea, didn't try it... 1. List item

EDIT

I tried this code and it worked. The key is to disable the default style width OverridesDefaultStyle="False"

<Window.Resources>
<Style TargetType="{x:Type Button}">
      <Setter Property="Background" Value="Blue"></Setter>
    <Style.Triggers>
        <EventTrigger RoutedEvent="Click">
            <EventTrigger.Actions>
                <BeginStoryboard>
                        <Storyboard BeginTime="00:00:00" 
                        RepeatBehavior="Forever" 
                                    AutoReverse="True"
                        Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)">
                            <ColorAnimation From="Black" To="Red" Duration="0:0:1"/>
                        </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Style.Triggers>
</Style>

</Window.Resources>
<Button Width="100" Height="35" OverridesDefaultStyle="False"  Focusable="False">dfdfdf</Button>

EDIT

Also add Focusable="False" to show the black to red color. Change the timings the way you like.

Upvotes: 4

Matt
Matt

Reputation: 7254

I seem to have problems in general to change the background color of a button, and this seems to be a widely shared issue. I instead came up with the following solution which works as well. It targets Opacity via DoubleAnimation and is pretty flexible.

<Style TargetType="{x:Type Button}">
        <Style.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                       <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.4" RepeatBehavior="30x"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>

Upvotes: -1

lokusking
lokusking

Reputation: 7456

You could try the following. I dont think this is very pretty, but matches your requirement without fading the colors:

 <Style TargetType="{x:Type Button}">
            <Style.Resources>
                <Storyboard x:Key="Storyboard" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)">
                    <ColorAnimationUsingKeyFrames  Duration="0:0:5" >
                        <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Black"></DiscreteColorKeyFrame>
                        <DiscreteColorKeyFrame KeyTime="0:0:2" Value="Red"></DiscreteColorKeyFrame>
                        <DiscreteColorKeyFrame KeyTime="0:0:3" Value="Black"></DiscreteColorKeyFrame>
                        <DiscreteColorKeyFrame KeyTime="0:0:4" Value="Red"></DiscreteColorKeyFrame>
                        <DiscreteColorKeyFrame KeyTime="0:0:5" Value="Black"></DiscreteColorKeyFrame>
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </Style.Resources>
            <Style.Triggers>
                <EventTrigger RoutedEvent="Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard Name="flash" Storyboard="{StaticResource Storyboard}"/>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>

Just change the colors in DiscreteColorKeyFrame as your need them

Upvotes: 2

Related Questions