Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Stopping a double animation

In my UserControl.Resources i have defined a Storyboard which triggers a DoubleAnimation to set the Opactiy.

<Storyboard AutoReverse="True" x:Key="BlinkingStoryBoard">
    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.1" Duration="0:0:2" RepeatBehavior="Forever"/>
</Storyboard>

This Storyboard is applied to a grid when the UserControl is loaded:

<Grid Name="ImagePlaceHolder">
    <Rectangle Fill="#D52B1E" Width="75" Height="75"/>
    <Image Source="pack://siteoforigin:,,,/Resources/Images/cross.png" Width="75" Height="75" RenderTransformOrigin="0.5, 0.5" x:Name="Cross"/>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="UserControl.Loaded">
            <BeginStoryboard Storyboard="{StaticResource BlinkingStoryBoard}"/>
        </EventTrigger>
    </Grid.Triggers>
</Grid>

From my code-behind I want to stop this animation, but I cant stop it!

((Storyboard)uc.FindResource("BlinkingStoryBoard")).Stop();

Any ideas?

Upvotes: 2

Views: 79

Answers (1)

Ben
Ben

Reputation: 3391

Have a look at this reference.

The main idea is that you need to call the storyboard Stop method passing the container of the storyboard.

You should try replacing the stop call with this:

((Storyboard)uc.FindResource("BlinkingStoryBoard")).Stop(ImagePlaceHolder);

Also from the reference, it shows that you can name your BeginStoryboard, for example:

<BeginStoryboard Name="MyStoryboardStarter" Storyboard="{StaticResource BlinkingStoryBoard}"/>

Now in the code behind you can find your storyboard more easily and stop it like this:

MyStoryboardStarter.Storyboard.Stop(ImagePlaceHolder);

Hope this helps.

Upvotes: 1

Related Questions