Ben Clarke
Ben Clarke

Reputation: 1061

How to make button and background flash in wpf

Issue


The issue I am having is when a user clicks a button I want the button to start flashing from 100% visible to around 20% visible and the background of the WPF window to change colors from #ffffff to #d6786a and this should happen untill the button is clicked again. Then the animations should stop.

Code


I have done a bit of diging around to find where somone has done this and I can't seem to see anything. The code i have at the moment is as follows.

Here is the button I want to flash from 100% visible to 20% visible when clicked:

<Button Name="button2" Style="{DynamicResource NoChromeButton}" Visibility="{Binding VisableState, Converter={StaticResource BoolToVis}}" ToolTip="Start Live Stream" Command="{Binding PlayStream}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="5" Height="22" Width="22" >
    <Image Source="../Images/recordicon.png"/>
</Button>

I have tried to add a storyboard into the button and it just didn't work:

<Button.Triggers>
  <EventTrigger RoutedEvent="Button.Click">
    <EventTrigger.Actions>
      <BeginStoryboard>
        <Storyboard BeginTime="00:00:00" 
                                RepeatBehavior="Forever" 
                                Storyboard.TargetName="button2" 
                                Storyboard.TargetProperty="(Visibility)">
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger.Actions>
  </EventTrigger>
</Button.Triggers>

I am new to WPF so going into animation is more difficult for me. If anyone could give me a little help on this stage it would be great.

Upvotes: 2

Views: 5617

Answers (2)

Sinatr
Sinatr

Reputation: 22008

To change "visibility to around 20%" you have to animate Opacity property (double type from 0.0 to 1.0) not Visibility (enum with 3 values, see msdn).

The goal can be easily achieved if you can use ToggleButton (because of its nice Checked and Unckecked routed events):

<ToggleButton>
    <ToggleButton.Triggers>
        <EventTrigger RoutedEvent="ToggleButton.Checked">
            <BeginStoryboard x:Name="storyboard">
                <Storyboard RepeatBehavior="Forever"
                            AutoReverse="True">
                    <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                     To="0.2"
                                     Duration="0:0:1" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="ToggleButton.Unchecked">
            <StopStoryboard BeginStoryboardName="storyboard" />
        </EventTrigger>
    </ToggleButton.Triggers>
</ToggleButton>

Upvotes: 0

Kapit&#225;n Ml&#237;ko
Kapit&#225;n Ml&#237;ko

Reputation: 4538

Define animations in resources for example like this

<Window x:Name="window" x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Storyboard x:Key="FlashButton" RepeatBehavior="Forever">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="button">
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="ChangeColor">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid">
            <EasingColorKeyFrame KeyTime="0:0:1" Value="Red"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="ChangeColor2">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid">
            <EasingColorKeyFrame KeyTime="0:0:1" Value="White"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>
<Grid x:Name="grid" Background="White">
    <Button x:Name="button" Content="Button" Margin="10,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75" Click="button_Click"/>

</Grid>

and create handler for button click

        private bool isRunning;
    private void button_Click(object sender, RoutedEventArgs e)
    {
        var flashButton = FindResource("FlashButton") as Storyboard;
        var changeColor = FindResource("ChangeColor") as Storyboard;
        var changeColor2 = FindResource("ChangeColor2") as Storyboard;

        if (isRunning)
        {
            flashButton.Stop();
            changeColor2.Begin();
            isRunning = false;
        }
        else
        {
            flashButton.Begin();
            changeColor.Begin();
            isRunning = true;
        }
    }

Upvotes: 3

Related Questions