How to Stop an Animation in C#/Wpf

How to stop an animation in C# / WPF?

To stop it, call BeginAnimation again with the second argument set to null.

How to stop an animation in WPF

When using Begin, Set the second parameter (IsControllable) to true in order to make the Stop method work:

_wheelAnimation.Begin(this, true);
_wheelAnimation.Stop(this);

Stop and start animation in WPF

You had not assigned the TargetName Property of the Storyboard to start the animation.

Well, there are two problems in your xaml to make the animation start.

1) Storyboard.TargetName="MyAnimation", Target name must be controls name. In your case, you have provided but you have given or assigned it to RotateTransform, which comes under the Image control, but not the image.

 <Image    x:Name="ImageControl" Grid.Row="0" MaxHeight="50" Source="C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"
RenderTransformOrigin=".5,.5" >

and in DoubleAnimation provide TargetName as the Image control's name

Storyboard.TargetName="ImageControl" 

2) Another problem is you have not provided the Target Property on which your double animation is dependent.

Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" 

Your stop code didn't work because, it was not started. Adding this below xaml will solve your problem.

Change in the speed is because you have given 600, for 6 Seconds it must be 6000 and not 600.

This is the change i have done. On Mouse Down event I have given to Stop StoryBoard.

     <Image.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard x:Name="BeginImageRotateAni" >
<Storyboard x:Name="MyStoryboard">
<DoubleAnimation x:Name="Prope11"
Storyboard.TargetName="ImageControl"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
To="360"
Duration="0:0:6"
RepeatBehavior="Forever"
FillBehavior="Stop"
>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseDown">
<EventTrigger.Actions>
<StopStoryboard BeginStoryboardName="BeginImageRotateAni"/>

</EventTrigger.Actions>
</EventTrigger>
</Image.Triggers>

and subscribed for MouseDownEvent in Constructor

  ImageControl.MouseDown += ImageControl_MouseDown;

and write the below code inside the event.

    bool IsAnimationstarted = true;

private void ImageControl_MouseDown(object sender, MouseButtonEventArgs e)
{
if (IsAnimationstarted)
{
MyStoryboard.Stop();
IsAnimationstarted = false;
}
else
{
TimeSpan ts = TimeSpan.FromMilliseconds(6000);
Prope11.Duration = ts;
MyStoryboard.Begin();
IsAnimationstarted = true;
}
}
}

Alternate solution:- by creating the animation in the resource and then utilizing it in the codebehind.

<Window.Resources>
<Storyboard x:Name="MyStoryboard" x:Key="Animation1" >
<DoubleAnimation x:Name="Prope11"
Storyboard.TargetName="ImageControl"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
To="360"
Duration="0:0:6"
RepeatBehavior="Forever"
FillBehavior="Stop"
>
</DoubleAnimation>
</Storyboard>
</Window.Resources>

In Code Behind:-

bool IsAnimationstarted = true;

private void ImageControl_MouseDown(object sender, MouseButtonEventArgs e)
{
Storyboard board = (Storyboard)this.FindResource("Animation1");
if (IsAnimationstarted)
{
IsAnimationstarted = false;
board.Begin();
}
else
{
IsAnimationstarted = true;
board.Pause();
}
}

I don't know why are changing the speed or not sure about your intention. What I have done here is when the user clicks on the image control, it will start the animation and pause again clicked and this goes on.

How to stop an animation WPF?

you can use this:

<Border Width="20" Height="20" Background="Red" x:Name="border" >
<Border.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard Name="Ali">
<Storyboard>
<DoubleAnimation To="0" Duration="0:0:4" Completed="com" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<StopStoryboard BeginStoryboardName="Ali"/>
</EventTrigger>
</Border.Triggers>
</Border>

and :

private void com(object sender, EventArgs e)
{
MessageBox.Show("boom!");
}

C# WPF Stop Storyboard

Have you tried to pause the anmation?

A good example is found here and when you want to pause it from the Code-Behind, use DataTriggerinstead of EventTrigger.

And maybe you are interested in the "Triple the speed of the Storyboard" too.


Edit
Your Question "I want to it to start / stop with a button not when a page is loaded. how can i solve this ?" cant be answered because when the Page (Border?) is not loaded, the animation cant be running. I Suggest to hide the Page/Border and only make it visible when the animation is paused.

I fiddled a bit around and tried to solve it with the Visibility

    <!-- This Border is animated. -->
<Border Height="300" Margin="68,434,1506,335" >
<Border.Style>
<Style TargetType="{x:Type Border}">
<!-- Here is your animation -->
<Style.Triggers>
<EventTrigger RoutedEvent="Border.Loaded">
<BeginStoryboard Name="RandomStoryboard">
<Storyboard >
<RectAnimation Storyboard.TargetProperty="Background.(ImageBrush.Viewport)"
To="0,0,1,1" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
<!-- Stop the animation at the Start -->
<PauseStoryboard BeginStoryboardName="RandomStoryboard" />
</EventTrigger>
<!-- Control the animation according to the Togglebutton State -->
<DataTrigger Binding="{Binding Path=IsChecked, ElementName=SpinControl}" Value="True">
<DataTrigger.EnterActions>
<ResumeStoryboard BeginStoryboardName="RandomStoryboard" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<PauseStoryboard BeginStoryboardName="RandomStoryboard" />
</DataTrigger.ExitActions>
<!-- Hide the Border while the animation is running -->
<Setter Property="Border.Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>

<!-- This Button Controls the Animated Border -->
<ToggleButton Name="SpinControl">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Content" Value="Start"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="Stop"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>

Note: The Style Section of the ToggleButtonis optional, it changes only the Content from Start to Stop (and vice versa).

Note2: Dont forget to insert your VisualBrush in the Border, otherwise the animation is not recognized.

Stopping an animation in WPF, dynamic loaded usercontrol

I have found a solution but it requires to remove the Trigger.

Step1: Put your animation into "UserControl.Resources" and give it the attribute "x:Key=sbMain"

Step2: Add an event to UserControl Loaded

Step3: In code behind change "sbMain.Stop();" to "((Storyboard)FindResource("sbMain")).Stop();", may you have to add some using directives.

Step4: To the LoadEvent add "((Storyboard)FindResource("sbMain")).Begin();"

The result looks something like this:

<UserControl x:Class="NotificationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Transparent" MouseEnter="UserControl_MouseEnter" Width="300" Height="200" Loaded="UserControl_Loaded">
<UserControl .Resources>
<Storyboard x:Key="sbMain">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" Storyboard.TargetName="AnimatedGrid">
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="AnimatedGrid">
<SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl .Resources>

<Grid RenderTransformOrigin="0,1" x:Name="AnimatedGrid">

<Border BorderThickness="1" Background="Beige" BorderBrush="Black" CornerRadius="10">
<StackPanel Margin="20">
<TextBlock TextWrapping="Wrap" Margin="5">
<Bold>Besked fra podio</Bold><LineBreak /><LineBreak />
Her skal der være en podio notification!
</TextBlock>
<CheckBox Content="Check check" Margin="5 5 0 5" />
<Button Content="Klik på mig" HorizontalAlignment="Center" />
</StackPanel>
</Border>

<Grid.RenderTransform>
<ScaleTransform ScaleY="1" />
</Grid.RenderTransform>

</Grid>

and in code behind:

private void UserControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
((Storyboard)FindResource("sbMain")).Stop();
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
((Storyboard)FindResource("sbMain")).Begin();
}

this code worked in my case.

Pausing/Stopping Gif animation in WPF

According to your documentation (which you should have read yourself) this will work:

private void Button_Click(object sender, RoutedEventArgs e)
{
var controller = ImageBehavior.GetAnimationController(this.scaner);

controller.Pause();
}

Please note that working directly with button functions is not the prefered way in WPF, if you want to use the full power I suggest using MVVM and the command pattern instead.

Stop WPF animation, storyboard begin in xaml but stopping it in codebehind?

I am thankful for Timothy for giving nice Idea. Here I am posting my working code

   /*create this resources as global to that perticular xaml. Need not to be put it in App.xaml
MyControl could be Window or Page or UserControl */

<MyControl.Resources>
<Storyboard x:Key="MovingServer" Storyboard.TargetName="MyImage" RepeatBehavior="Forever" >
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="30" To="300" BeginTime="0:0:0" />
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:5" From="300" To="300" BeginTime="0:0:5" />
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" Duration="0:0:2" From="300" To="600" BeginTime="0:0:7" />
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2" From="1" To="0" BeginTime="0:0:7" />
</Storyboard>
</MyControl.Resources>

/* <!-- Now use those animation resources, the place where you want. You can use it as static resource and begin stop animation from code behind OR use it as trigger event --> */

/* <!-- Static resources--> */
<Canvas>
<Image Canvas.Left="0" Canvas.Top="-2" Height="32" Name="MyImage" Width="32" Source="/CCTrayHelper;component/Images/ServerIcon.png" Visibility="Hidden"/>
<Canvas.Resources>
<BeginStoryboard x:Key="serverAnimate" Storyboard="{StaticResource MovingServer}" />
</Canvas.Resources>
</Canvas>
<Button x:Name="ScanButton" onClick="Scanbutton_Click" />

/* ****************************************************************** */

/* Code behind to start/stop animation*/

//Get the resource value first on current object, so that when you start/stop the animation, it work only on current object
Storyboard sbImageAnimate = (Storyboard)this.ServerView.FindResource("MovingServer");

//Start the animation on Button Click
protected void Scanbutton_Click(object Sender, EventArgs e)
{
this.MyImage.Visibility = System.Windows.Visibility.Visible;
sbImageAnimate.Begin();
}

//Stop animation on my own even. You can use it on any event
private void EventPublisher_OnFinish(object sender, EventArgs args)
{
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() { this.StopScanningAnimation(); });
}

private void StopScanningAnimation()
{
sbImageAnimate.Stop();
this.MyImage.Visibility = System.Windows.Visibility.Hidden;
}


Related Topics



Leave a reply



Submit