Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF animation that is always running when the control is visible

Tags:

animation

wpf

I have a (lame) user requirement to make a control super visible.

Sadly that means a flashing background (Ug).

So, the control is a Border that holds a TextBlock is only visible in fairly rare scenarios.

I have looked at a few animation examples and they all have a "Trigger" on them. Most commonly when the user clicks on something.

Is there a way to just have the animation running all the time (if the control is visible of course)?

like image 777
Vaccano Avatar asked Nov 26 '25 03:11

Vaccano


1 Answers

here you go, RepeatBehavior="Forever" will keep the animation running until stopped or removed

you can trigger a color animation with auto reverse enabled on the control load and let it run forever

<Border Background="Transparent">
    <TextBlock Text="some text" />
    <Border.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation To="SkyBlue"
                                    Storyboard.TargetProperty="Background.Color"
                                    RepeatBehavior="Forever"
                                    AutoReverse="True" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Border.Triggers>
</Border>

if you need the animation to be triggered on visibility change then here is a way, note that the animation is applied when IsVisible property become true and stopped when it become false.

<Border Background="Transparent">
    <TextBlock Text="some text" />
    <Border.Style>
        <Style TargetType="Border">
            <Style.Triggers>
                <Trigger Property="IsVisible"
                         Value="true">
                    <Trigger.EnterActions>
                        <BeginStoryboard x:Name="startFlashing">
                            <Storyboard>
                                <ColorAnimation To="SkyBlue"
                                                Storyboard.TargetProperty="Background.Color"
                                                RepeatBehavior="Forever"
                                                AutoReverse="True" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="startFlashing" />
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>

typically after visibility is set to false there is no visible difference if animation is still running or stopped.

like image 194
pushpraj Avatar answered Nov 28 '25 01:11

pushpraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!