Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a WPF Label (or other element) flash using animation

I have a label that I only make visible based on one of my ViewModel Properties. Here is the XAML:

<Label  HorizontalAlignment="Center" VerticalAlignment="Center"
        HorizontalContentAlignment="Center" 
        VerticalContentAlignment="Center" 
        FontSize="24" Width="200" Height="200" >
    <Label.Content >
        Option in the money! 
    </Label.Content>
    <Label.Style>
        <Style TargetType="{x:Type Label}">
            <Setter Property="Visibility" Value="Hidden" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding OptionInMoney}" Value="True">
                    <Setter Property="Visibility"
                Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label.Style>

</Label>

I'm not sure this is the best way, but in any case, I'd also like to have the label flashing. Clearly, I only want it flashing when it is visible. Can someone point me to some example code, or write a quick example to do this? I assume I need some sort of trigger, and an animation. Presumably I also need a trigger when the label is no longer visible so that I stop the animation?

Thanks, Dave P.S. Is there a good book or site for all these WPF tricks? Something like the "MFC Answer Book" for those that remember that book.

like image 815
Dave Avatar asked Apr 04 '13 22:04

Dave


People also ask

How do I add a label in WPF?

Creating a WPF LabelThe Width and Height attributes of the Label element represent the width and the height of a Label. The Content property of the Label element sets the text of a Label. The Name attribute represents the name of the control, which is a unique identifier of a control.

What is WPF animation?

WPF Property Animation System Most important is that, in WPF, you animate objects by applying animation to their individual properties. For example, to make a framework element grow, you animate its Width and Height properties. To make an object fade from view, you animate its Opacity property.

What is storyboard WPF?

The Storyboard class provides the Storyboard. TargetName and Storyboard. TargetProperty attached properties. You set these properties on an animation to specify its target object and property. To apply animations to their targets, you begin the Storyboard using a trigger action or a method.

What is label in XAML?

A XAML Label element represents a Label control. Code sample of how to use a XAML Label. This article has moved here: Working with WPF Label Control using XAML and C# The code example in Listing 1 creates a Label control in XAML and sets the name, height, width and content of a Label control.


1 Answers

You could add a Storyboard animation to the Style.Resources and start it in the EnterActions section of the DataTrigger.

A simple DoubleAnimation on the Opacity should work fine

Something like this:

<Label.Style>
    <Style TargetType="{x:Type Label}">
        <Style.Resources>
            <Storyboard x:Key="flashAnimation" >
                <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" AutoReverse="True" Duration="0:0:0.5" RepeatBehavior="Forever" />
            </Storyboard>
        </Style.Resources>

        <Setter Property="Visibility" Value="Hidden" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding OptionInMoney}" Value="True">
                <Setter Property="Visibility" Value="Visible" />
                <DataTrigger.EnterActions>
                    <BeginStoryboard Name="flash" Storyboard="{StaticResource flashAnimation}" />
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <StopStoryboard BeginStoryboardName="flash"/>
                </DataTrigger.ExitActions>
            </DataTrigger>

        </Style.Triggers>
    </Style>
</Label.Style>
like image 175
sa_ddam213 Avatar answered Oct 20 '22 00:10

sa_ddam213