Top 28 Wpf Doubleanimation All Answers

You are looking for information, articles, knowledge about the topic nail salons open on sunday near me wpf doubleanimation on Google, you do not find the information you need! Here are the best content compiled and compiled by the https://chewathai27.com/to team, along with other related topics such as: wpf doubleanimation Storyboard template wpf, Storyboard WPF, WPF animation, WPF ScaleTransform animation, Gif WPF, wpf storyboard, Loading screen wpf, WPF sample

What is DoubleAnimation in WPF?

Example: Make an Element Fade In and Out of View. This example shows how to use a WPF animation to animate the value of a dependency property. It uses a DoubleAnimation, which is a type of animation that generates Double values, to animate the Opacity property of a Rectangle.

What is StoryBoard WPF?

Storyboard objects use name scopes to resolve the TargetName property. For more information about WPF name scopes, see WPF XAML Namescopes. If the TargetName property is omitted, the animation targets the element on which it is defined, or, in the case of styles, the styled element.

What is double animation?

DoubleAnimation(Double, Double, Duration, FillBehavior) Initializes a new instance of the DoubleAnimation class that animates from the specified starting value to the specified destination value over the specified duration and has the specified fill behavior. DoubleAnimation(Double, Duration)

Can you animate without StoryBoard?

Animations can be applied without using the StoryBoard. BeginAnimation() method can be used to apply animations instead of StoryBoard. This method can be used when simple animations are applied to a property of a control.

What is WPF vs Windows Forms?

WPF (Windows Presentation Foundation): WPF, as the name suggests, is a UI framework used for developing Windows or desktop client applications.

Difference between WPF and WinForms.
WPF WinForms
It can render fast as compared to WinForms, complexity, and support. It renders slow as compared to WPF.
26 thg 4, 2022

What is WPF canvas?

Advertisements. Canvas panel is the basic layout Panel in which the child elements can be positioned explicitly using coordinates that are relative to the Canvas any side such as left, right, top and bottom.

What is routed events in WPF with example?

A routed event is an event registered with the WPF event system, backed by an instance of the RoutedEvent class, and processed by the WPF event system. The RoutedEvent instance, obtained from registration, is typically stored as a public static readonly member of the class that registered it.

Is storyboarding difficult?

All the art skills apply to storyboarding. Storyboarding is hard work because it is creative work.

Which type of animation is faster and easier to make?

Digital 3d animation characters are much faster to create and they are quite popular in the movie making industry. Using a computer software 3d animated images are used to create many short films, full length movies and even tv commercials and a career in digital 3d animation is highly rewarding.

How long should a storyboard take?

Here are the facts: every panel for a television storyboard based on a written script takes 10 – 20 minutes AVERAGE per panel WITHOUT revision time included.

What are the 3 types of animation?

Different Types of Animation:

Traditional Animation. 2D Animation (Vector-based) 3D Animation. Motion Graphics.

What are the 5 types of animation?

5 Forms of Animation
  • Traditional Animation.
  • 2D Animation.
  • 3D Animation.
  • Motion Graphics.
  • Stop Motion.

What is Sakuga anime?

Noun. sakuga (uncountable) In Japanese animation (anime), a sequence of noticeably higher quality, used to highlight a particularly important scene.

What are the 4 types of animation?

There are four types of animation effects in PowerPoint – entrance, emphasis, exit and motion paths. These reflect the point at which you want the animation to occur.


WPF Animations Basics – Double Animation
WPF Animations Basics – Double Animation


WPF DoubleAnimation – Dot Net Perls

  • Article author: www.dotnetperls.com
  • Reviews from users: 12512 ⭐ Ratings
  • Top rated: 4.3 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about WPF DoubleAnimation – Dot Net Perls DoubleAnimation. WPF supports animation of elements. To move an element, we can use a canvas and adjust its position within the canvas. We introduce the … …
  • Most searched keywords: Whether you are looking for WPF DoubleAnimation – Dot Net Perls DoubleAnimation. WPF supports animation of elements. To move an element, we can use a canvas and adjust its position within the canvas. We introduce the … Use the DoubleAnimation element in XAML to animate a Rectangle upon a Canvas element. The Rectangle moves.
  • Table of Contents:
WPF DoubleAnimation - Dot Net Perls
WPF DoubleAnimation – Dot Net Perls

Read More

animation – WPF doubleanimation : animate in steps? – Stack Overflow

  • Article author: stackoverflow.com
  • Reviews from users: 46251 ⭐ Ratings
  • Top rated: 4.6 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about animation – WPF doubleanimation : animate in steps? – Stack Overflow From MSDN, the definition of the By property is ‘Gets or sets the total amount by which the animation changes its starting value.’ It continues, ‘Use the By … …
  • Most searched keywords: Whether you are looking for animation – WPF doubleanimation : animate in steps? – Stack Overflow From MSDN, the definition of the By property is ‘Gets or sets the total amount by which the animation changes its starting value.’ It continues, ‘Use the By …
  • Table of Contents:

3 Answers
3

Your Answer

Not the answer you’re looking for Browse other questions tagged wpf animation or ask your own question

animation - WPF doubleanimation : animate in steps? - Stack Overflow
animation – WPF doubleanimation : animate in steps? – Stack Overflow

Read More

Animation Overview – WPF .NET Framework | Microsoft Docs

  • Article author: docs.microsoft.com
  • Reviews from users: 46756 ⭐ Ratings
  • Top rated: 3.5 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about Animation Overview – WPF .NET Framework | Microsoft Docs Updating …
  • Most searched keywords: Whether you are looking for Animation Overview – WPF .NET Framework | Microsoft Docs Updating Make an attractive user interface even more spectacular with dramatic screen transitions or vivid visual cues in Windows Presentation Foundation (WPF).
  • Table of Contents:

Introducing Animations

WPF Property Animation System

Example Make an Element Fade In and Out of View

Animation Types

Applying an Animation to a Property

Interactively Control a Storyboard

What Happens After an Animation Ends

Data Binding and Animating Animations

Other Ways to Animate

Animation Samples

Related Topics

Reference

Feedback

Animation Overview - WPF .NET Framework | Microsoft Docs
Animation Overview – WPF .NET Framework | Microsoft Docs

Read More

Storyboards Overview – WPF .NET Framework | Microsoft Docs

  • Article author: docs.microsoft.com
  • Reviews from users: 25772 ⭐ Ratings
  • Top rated: 3.4 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about Storyboards Overview – WPF .NET Framework | Microsoft Docs Updating …
  • Most searched keywords: Whether you are looking for Storyboards Overview – WPF .NET Framework | Microsoft Docs Updating Organize and apply animations in storyboards. Use property-targeting syntax and combine timelines in Windows Presentation Foundation (WPF).
  • Table of Contents:

Prerequisites

What Is a Storyboard

Where Can You Use a Storyboard

How to Apply Animations with a Storyboard

Targeting Framework Elements Framework Content Elements and Freezables

Indirect Targeting

Interactively Controlling a Storyboard in XAML

Interactively Controlling a Storyboard by Using Code

Animate in a Style

Animate in a ControlTemplate

Animate When a Property Value Changes

See also

Feedback

Storyboards Overview - WPF .NET Framework | Microsoft Docs
Storyboards Overview – WPF .NET Framework | Microsoft Docs

Read More

DoubleAnimation Class (System.Windows.Media.Animation) | Microsoft Docs

  • Article author: docs.microsoft.com
  • Reviews from users: 29032 ⭐ Ratings
  • Top rated: 4.5 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about DoubleAnimation Class (System.Windows.Media.Animation) | Microsoft Docs Updating …
  • Most searched keywords: Whether you are looking for DoubleAnimation Class (System.Windows.Media.Animation) | Microsoft Docs Updating Animates the value of a Double property between two target values using linear interpolation over a specified Duration.
  • Table of Contents:

Definition

Remarks

Constructors

Fields

Properties

Methods

Events

Applies to

See also

DoubleAnimation Class (System.Windows.Media.Animation) | Microsoft Docs
DoubleAnimation Class (System.Windows.Media.Animation) | Microsoft Docs

Read More

How do I apply an animation without using a storyboard ? | WPF FAQ | Syncfusion

  • Article author: www.syncfusion.com
  • Reviews from users: 35745 ⭐ Ratings
  • Top rated: 4.4 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about How do I apply an animation without using a storyboard ? | WPF FAQ | Syncfusion Updating …
  • Most searched keywords: Whether you are looking for How do I apply an animation without using a storyboard ? | WPF FAQ | Syncfusion Updating This FAQ explains the topic ” How do I apply an animation without using a storyboard ? “. You can submit your questions here .
  • Table of Contents:

UK +44 20 7084 6215

Toll Free 1-888-9DOTNET

CONTACT US

How do I apply an animation without using a storyboard ? | WPF FAQ | Syncfusion
How do I apply an animation without using a storyboard ? | WPF FAQ | Syncfusion

Read More

Better Animations (Winforms, Wpf)

  • Article author: viblo.asia
  • Reviews from users: 10557 ⭐ Ratings
  • Top rated: 4.6 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about Better Animations (Winforms, Wpf) Animations in wpf can be a headache sometimes. Not many clear articles on it so ill make this article on how one can create animation and bypass those … …
  • Most searched keywords: Whether you are looking for Better Animations (Winforms, Wpf) Animations in wpf can be a headache sometimes. Not many clear articles on it so ill make this article on how one can create animation and bypass those … Animations, WPF, C#Animations in wpf can be a headache sometimes. Not many clear articles on it so ill make this article on how one can create animation class and bypass those anoying xml storyboards (and all tags withi…
  • Table of Contents:
Better Animations (Winforms, Wpf)
Better Animations (Winforms, Wpf)

Read More

Animation in WPF Application

  • Article author: www.c-sharpcorner.com
  • Reviews from users: 48975 ⭐ Ratings
  • Top rated: 4.9 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about Animation in WPF Application Animation is one of the finest features in a WPF application which give us the ability to create many types of animations. …
  • Most searched keywords: Whether you are looking for Animation in WPF Application Animation is one of the finest features in a WPF application which give us the ability to create many types of animations. Animation in WPF Application,Rotated Rectangle in WPF,WPF,WPF Animation,WPF tutorialsAnimation is one of the finest features in a WPF application which give us the ability to create many types of animations. Here we will make a rectangle that will rotate at a fixed position.
  • Table of Contents:
Animation in WPF Application
Animation in WPF Application

Read More


See more articles in the same category here: Chewathai27.com/to/blog.

WPF DoubleAnimation

WPF DoubleAnimationUse the DoubleAnimation element in XAML to animate a Rectangle upon a Canvas element. The Rectangle moves.

DoubleAnimation. WPF supports animation of elements. To move an element, we can use a canvas and adjust its position within the canvas. We introduce the BeginStoryboard, Storyboard and DoubleAnimation elements.

Animation can be implemented with C# code, but in the simplest case this is not necessary. Here I add a Canvas element to the Window. I specify a single Rectangle element, and adjust its Fill, Stroke, StrokeThickness, Width and Height. Triggers I add a sub-element to the Rectangle called Rectangle.Triggers. This is complicated, but you just have to remember the elements. RoutedEvent The animation begins when the Loaded event is triggered on the Window. We use EventTrigger for this. with C# code, but in the simplest case this is not necessary. Here I add a Canvas element to the Window. I specify a single Rectangle element, and adjust its Fill, Stroke, StrokeThickness, Width and Height.

In BeginStoryboard, we add a Storyboard element. And in a nested DoubleAnimation, we specify how the animation occurs. We indicate the target element’s name, what property it affects, From, To and a Duration. we add a Storyboard element. And in a nested DoubleAnimation, we specify how the animation occurs. We indicate the target element’s name, what property it affects, From, To and a Duration.

Example markup, XAML: Rectangle Name=”Rect1″ Canvas.Left=”10″ Fill=”LightSeaGreen” Stroke=”Bisque” StrokeThickness=”5″ Width=”100″ Height=”100″>

When this program is executed, the rectangle will creep across the window towards the right. It does this in two seconds. You can change the Duration to make it move slower (use a higher number) or faster (use a lower one). From, To We can adjust the “From” and the “To” to change the animation’s start and end values. These are used for the TargetProperty. Property Syntax Please add the parentheses around the TargetProperty in your XAML. If you do not, an error (a confusing one) will occur. the rectangle will creep across the window towards the right. It does this in two seconds. You can change the Duration to make it move slower (use a higher number) or faster (use a lower one).

Discussion. There are other “Animation” elements. Similar to DoubleAnimation, we can employ DecimalAnimation to manipulate a floating-point property. ByteAnimation is similar. And ColorAnimation can change a color, creating fade effects. Tip These elements at first make WPF seem excessively complicated and impenetrable. But when needed, they make programs simpler. There are other “Animation” elements. Similar to DoubleAnimation, we can employ DecimalAnimation to manipulate a floating-point property. ByteAnimation is similar. And ColorAnimation can change a color, creating fade effects.

Summary. With WPF, we gain a large set of graphical effects. This is a key advantage of WPF over older technologies such as Windows Forms. With Storyboards, we can specify, declaratively, the nature of our animations.

Animation Overview – WPF .NET Framework

Table of contents

Animation Overview

Article

03/17/2022

20 minutes to read

1 contributor

In this article

Windows Presentation Foundation (WPF) provides a powerful set of graphics and layout features that enable you to create attractive user interfaces and appealing documents. Animation can make an attractive user interface even more spectacular and usable. By just animating a background color or applying an animated Transform, you can create dramatic screen transitions or provide helpful visual cues.

This overview provides an introduction to the WPF animation and timing system. It focuses on the animation of WPF objects by using storyboards.

Introducing Animations

Animation is an illusion that is created by quickly cycling through a series of images, each slightly different from the last. The brain perceives the group of images as a single changing scene. In film, this illusion is created by using cameras that record many photographs, or frames, each second. When the frames are played back by a projector, the audience sees a moving picture.

Animation on a computer is similar. For example, a program that makes a drawing of a rectangle fade out of view might work as follows.

The program creates a timer.

The program checks the timer at set intervals to see how much time has elapsed.

Each time the program checks the timer, it computes the current opacity value for the rectangle based on how much time has elapsed.

The program then updates the rectangle with the new value and redraws it.

Prior to WPF, Microsoft Windows developers had to create and manage their own timing systems or use special custom libraries. WPF includes an efficient timing system that is exposed through managed code and WPF framework. WPF animation makes it easy to animate controls and other graphical objects.

WPF handles all the behind-the-scenes work of managing a timing system and redrawing the screen efficiently. It provides timing classes that enable you to focus on the effects you want to create, instead of the mechanics of achieving those effects. WPF also makes it easy to create your own animations by exposing animation base classes from which your classes can inherit, to produce customized animations. These custom animations gain many of the performance benefits of the standard animation classes.

WPF Property Animation System

If you understand a few important concepts about the timing system, WPF animations can be easier to use. 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.

For a property to have animation capabilities, it must meet the following three requirements:

It must be a dependency property.

It must belong to a class that inherits from DependencyObject and implements the IAnimatable interface.

There must be a compatible animation type available. (If WPF does not provide one, you can create your own. See the Custom Animations Overview.)

WPF contains many objects that have IAnimatable properties. Controls such as Button and TabControl, and also Panel and Shape objects inherit from DependencyObject. Most of their properties are dependency properties.

You can use animations almost anywhere, which includes in styles and control templates. Animations do not have to be visual; you can animate objects that are not part of the user interface if they meet the criteria that are described in this section.

Example: Make an Element Fade In and Out of View

This example shows how to use a WPF animation to animate the value of a dependency property. It uses a DoubleAnimation, which is a type of animation that generates Double values, to animate the Opacity property of a Rectangle. As a result, the Rectangle fades in and out of view.

The first part of the example creates a Rectangle element. The steps that follow show how to create an animation and apply it to the rectangle’s Opacity property.

The following shows how to create a Rectangle element in a StackPanel in XAML.

The following shows how to create a Rectangle element in a StackPanel in code.

var myPanel = new StackPanel(); myPanel.Margin = new Thickness(10); var myRectangle = new Rectangle(); myRectangle.Name = “myRectangle”; this.RegisterName(myRectangle.Name, myRectangle); myRectangle.Width = 100; myRectangle.Height = 100; myRectangle.Fill = Brushes.Blue; myPanel.Children.Add(myRectangle); this.Content = myPanel;

Dim myPanel As New StackPanel() myPanel.Margin = New Thickness(10) Dim myRectangle As New Rectangle() myRectangle.Name = “myRectangle” Me.RegisterName(myRectangle.Name, myRectangle) myRectangle.Width = 100 myRectangle.Height = 100 myRectangle.Fill = Brushes.Blue myPanel.Children.Add(myRectangle) Me.Content = myPanel

Part 1: Create a DoubleAnimation

One way to make an element fade in and out of view is to animate its Opacity property. Because the Opacity property is of type Double, you need an animation that produces double values. A DoubleAnimation is one such animation. A DoubleAnimation creates a transition between two double values. To specify its starting value, you set its From property. To specify its ending value, you set its To property.

An opacity value of 1.0 makes the object completely opaque, and an opacity value of 0.0 makes it completely invisible. To make the animation transition from 1.0 to 0.0 you set its From property to 1.0 and its To property to 0.0 . The following shows how to create a DoubleAnimation in XAML. The following shows how to create a DoubleAnimation in code. var myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 1.0; myDoubleAnimation.To = 0.0; Dim myDoubleAnimation As New DoubleAnimation() myDoubleAnimation.From = 1.0 myDoubleAnimation.To = 0.0 Next, you must specify a Duration. The Duration of an animation specifies how long it takes to go from its starting value to its destination value. The following shows how to set the Duration to five seconds in XAML. The following shows how to set the Duration to five seconds in code. myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5)); myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(5)) The previous code showed an animation that transitions from 1.0 to 0.0 , which causes the target element to fade from completely opaque to completely invisible. To make the element fade back into view after it vanishes, set the AutoReverse property of the animation to true . To make the animation repeat indefinitely, set its RepeatBehavior property to Forever. The following shows how to set the AutoReverse and RepeatBehavior properties in XAML. The following shows how to set the AutoReverse and RepeatBehavior properties in code. myDoubleAnimation.AutoReverse = true; myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever; myDoubleAnimation.AutoReverse = True myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever

Part 2: Create a Storyboard

To apply an animation to an object, you create a Storyboard and use the TargetName and TargetProperty attached properties to specify the object and property to animate.

Create the Storyboard and add the animation as its child. The following shows how to create the Storyboard in XAML. To create the Storyboard in code, declare a Storyboard variable at the class level. public partial class MainWindow : Window { private Storyboard myStoryboard; Class MainWindow Private myStoryboard As Storyboard Then initialize the Storyboard and add the animation as its child. myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); myStoryboard = New Storyboard() myStoryboard.Children.Add(myDoubleAnimation) The Storyboard has to know where to apply the animation. Use the Storyboard.TargetName attached property to specify the object to animate. The following shows how to set the target name of the DoubleAnimation to MyRectangle in XAML. The following shows how to set the target name of the DoubleAnimation to MyRectangle in code. Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name); Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name) Use the TargetProperty attached property to specify the property to animate. The following shows how the animation is configured to target the Opacity property of the Rectangle in XAML. The following shows how the animation is configured to target the Opacity property of the Rectangle in code. Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty)); Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.OpacityProperty))

For more information about TargetProperty syntax and for additional examples, see the Storyboards Overview.

Part 3 (XAML): Associate the Storyboard with a Trigger

The easiest way to apply and start a Storyboard in XAML is to use an event trigger. This section shows how to associate the Storyboard with a trigger in XAML.

Create a BeginStoryboard object and associate your storyboard with it. A BeginStoryboard is a type of TriggerAction that applies and starts a Storyboard. Create an EventTrigger and add the BeginStoryboard to its Actions collection. Set the RoutedEvent property of the EventTrigger to the routed event that you want to start the Storyboard. (For more information about routed events, see the Routed Events Overview.) Add the EventTrigger to the Triggers collection of the Rectangle.

Part 3 (Code): Associate the Storyboard with an Event Handler

The easiest way to apply and start a Storyboard in code is to use an event handler. This section shows how to associate the Storyboard with an event handler in code.

Register for the Loaded event of the rectangle. myRectangle.Loaded += new RoutedEventHandler(myRectangleLoaded); AddHandler myRectangle.Loaded, AddressOf myRectangleLoaded Declare the event handler. In the event handler, use the Begin method to apply the storyboard. private void myRectangleLoaded(object sender, RoutedEventArgs e) { myStoryboard.Begin(this); } Private Sub myRectangleLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs) myStoryboard.Begin(Me) End Sub

Complete Example

The following shows how to create a rectangle that fades in and out of view in XAML.

The following shows how to create a rectangle that fades in and out of view in code.

using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Media.Animation; namespace WpfApplication1 { public partial class MainWindow : Window { private Storyboard myStoryboard; public MainWindow() { InitializeComponent(); StackPanel myPanel = new StackPanel(); myPanel.Margin = new Thickness(10); Rectangle myRectangle = new Rectangle(); myRectangle.Name = “myRectangle”; this.RegisterName(myRectangle.Name, myRectangle); myRectangle.Width = 100; myRectangle.Height = 100; myRectangle.Fill = Brushes.Blue; DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 1.0; myDoubleAnimation.To = 0.0; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5)); myDoubleAnimation.AutoReverse = true; myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever; myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty)); // Use the Loaded event to start the Storyboard. myRectangle.Loaded += new RoutedEventHandler(myRectangleLoaded); myPanel.Children.Add(myRectangle); this.Content = myPanel; } private void myRectangleLoaded(object sender, RoutedEventArgs e) { myStoryboard.Begin(this); } } }

Imports System.Windows.Media.Animation Class MainWindow Private myStoryboard As Storyboard Public Sub New() InitializeComponent() Dim myPanel As New StackPanel() myPanel.Margin = New Thickness(10) Dim myRectangle As New Rectangle() myRectangle.Name = “myRectangle” Me.RegisterName(myRectangle.Name, myRectangle) myRectangle.Width = 100 myRectangle.Height = 100 myRectangle.Fill = Brushes.Blue Dim myDoubleAnimation As New DoubleAnimation() myDoubleAnimation.From = 1.0 myDoubleAnimation.To = 0.0 myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(5)) myDoubleAnimation.AutoReverse = True myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever myStoryboard = New Storyboard() myStoryboard.Children.Add(myDoubleAnimation) Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name) Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.OpacityProperty)) ‘ Use the Loaded event to start the Storyboard. AddHandler myRectangle.Loaded, AddressOf myRectangleLoaded myPanel.Children.Add(myRectangle) Me.Content = myPanel End Sub Private Sub myRectangleLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs) myStoryboard.Begin(Me) End Sub End Class

Animation Types

Because animations generate property values, different animation types exist for different property types. To animate a property that takes a Double, such as the Width property of an element, use an animation that produces Double values. To animate a property that takes a Point, use an animation that produces Point values, and so on. Because of the number of different property types, there are several animation classes in the System.Windows.Media.Animation namespace. Fortunately, they follow a strict naming convention that makes it easy to differentiate between them:

Animation Known as a “From/To/By” or “basic” animation, these animate between a starting and destination value, or by adding an offset value to its starting value. To specify a starting value, set the From property of the animation. To specify an ending value, set the To property of the animation. To specify an offset value, set the By property of the animation. The examples in this overview use these animations, because they are the simplest to use. From/To/By animations are described in detail in the From/To/By Animations Overview.

AnimationUsingKeyFrames Key frame animations are more powerful than From/To/By animations because you can specify any number of target values and even control their interpolation method. Some types can only be animated with key frame animations. Key frame animations are described in detail in the Key-Frame Animations Overview.

AnimationUsingPath Path animations enable you to use a geometric path in order to produce animated values.

AnimationBase Abstract class that, when you implement it, animates a value. This class serves as the base class for Animation and AnimationUsingKeyFrames classes. You have to deal directly with these classes only if you want to create your own custom animations. Otherwise, use a Animation or KeyFrameAnimation.

In most cases, you will want to use the Animation classes, such as DoubleAnimation and ColorAnimation.

The following table shows several common animation types and some properties with which they are used.

Property type Corresponding basic (From/To/By) animation Corresponding key frame animation Corresponding Path Animation Usage example Color ColorAnimation ColorAnimationUsingKeyFrames None Animate the Color of a SolidColorBrush or a GradientStop. Double DoubleAnimation DoubleAnimationUsingKeyFrames DoubleAnimationUsingPath Animate the Width of a DockPanel or the Height of a Button. Point PointAnimation PointAnimationUsingKeyFrames PointAnimationUsingPath Animate the Center position of an EllipseGeometry. String None StringAnimationUsingKeyFrames None Animate the Text of a TextBlock or the Content of a Button.

Animations Are Timelines

All the animation types inherit from the Timeline class; therefore, all animations are specialized types of timelines. A Timeline defines a segment of time. You can specify the timing behaviors of a timeline: its Duration, how many times it is repeated, and even how fast time progresses for it.

Because an animation is a Timeline, it also represents a segment of time. An animation also calculates output values as it progresses through its specified segment of time (or Duration). As the animation progresses, or “plays,” it updates the property that it is associated with.

Three frequently used timing properties are Duration, AutoReverse, and RepeatBehavior.

The Duration Property

As previously mentioned, a timeline represents a segment of time. The length of that segment is determined by the Duration of the timeline, which is usually specified by using a TimeSpan value. When a timeline reaches the end of its duration, it has completed an iteration.

An animation uses its Duration property to determine its current value. If you do not specify a Duration value for an animation, it uses 1 second, which is the default.

The following syntax shows a simplified version of the Extensible Application Markup Language (XAML) attribute syntax for the Duration property.

hours : minutes : seconds

The following table shows several Duration settings and their resulting values.

Setting Resulting value 0:0:5.5 5.5 seconds. 0:30:5.5 30 minutes and 5.5 seconds. 1:30:5.5 1 hour, 30 minutes, and 5.5 seconds.

One way to specify a Duration in code is to use the FromSeconds method to create a TimeSpan, then declare a new Duration structure using that TimeSpan.

For more information about Duration values and the complete Extensible Application Markup Language (XAML) syntax, see the Duration structure.

AutoReverse

The AutoReverse property specifies whether a timeline plays backward after it reaches the end of its Duration. If you set this animation property to true , an animation reverses after it reaches the end of its Duration, playing from its ending value back to its starting value. By default, this property is false .

RepeatBehavior

The RepeatBehavior property specifies how many times a timeline plays. By default, timelines have an iteration count of 1.0 , which means they play one time and do not repeat at all.

For more information about these properties and others, see the Timing Behaviors Overview.

Applying an Animation to a Property

The previous sections describe the different types of animations and their timing properties. This section shows how to apply the animation to the property that you want to animate. Storyboard objects provide one way to apply animations to properties. A Storyboard is a container timeline that provides targeting information for the animations it contains.

Targeting Objects and Properties

The Storyboard class provides the TargetName and TargetProperty attached properties. By setting these properties on an animation, you tell the animation what to animate. However, before an animation can target an object, the object must usually be given a name.

Assigning a name to a FrameworkElement differs from assigning a name to a Freezable object. Most controls and panels are framework elements; however, most purely graphical objects, such as brushes, transforms, and geometries, are freezable objects. If you are not sure whether a type is a FrameworkElement or a Freezable, refer to the Inheritance Hierarchy section of its reference documentation.

To make a FrameworkElement an animation target, you give it a name by setting its Name property. In code, you must also use the RegisterName method to register the element name with the page to which it belongs.

To make a Freezable object an animation target in XAML, you use the x:Name Directive to assign it a name. In code, you just use the RegisterName method to register the object with the page to which it belongs.

The sections that follow provide an example of naming an element in XAML and code. For more detailed information about naming and targeting, see the Storyboards Overview.

Applying and Starting Storyboards

To start a storyboard in XAML, you associate it with an EventTrigger. An EventTrigger is an object that describes what actions to take when a specified event occurs. One of those actions can be a BeginStoryboard action, which you use to start your storyboard. Event triggers are similar in concept to event handlers because they enable you to specify how your application responds to a particular event. Unlike event handlers, event triggers can be fully described in XAML; no other code is required.

To start a Storyboard in code, you can use an EventTrigger or use the Begin method of the Storyboard class.

Interactively Control a Storyboard

The previous example showed how to start a Storyboard when an event occurs. You can also interactively control a Storyboard after it starts: you can pause, resume, stop, advance it to its fill period, seek, and remove the Storyboard. For more information and an example that shows how to interactively control a Storyboard, see the Storyboards Overview.

What Happens After an Animation Ends?

The FillBehavior property specifies how a timeline behaves when it ends. By default, a timeline starts Filling when it ends. An animation that is Filling holds its final output value.

The DoubleAnimation in the previous example does not end because its RepeatBehavior property is set to Forever. The following example animates a rectangle by using a similar animation. Unlike the previous example, the RepeatBehavior and AutoReverse properties of this animation are left at their default values. Therefore, the animation progresses from 1 to 0 over five seconds and then stops.

DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 1.0; myDoubleAnimation.To = 0.0; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));

Dim myDoubleAnimation As New DoubleAnimation() myDoubleAnimation.From = 1.0 myDoubleAnimation.To = 0.0 myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(5))

Because its FillBehavior was not changed from its default value, which is HoldEnd, the animation holds its final value, 0, when it ends. Therefore, the Opacity of the rectangle remains at 0 after the animation ends. If you set the Opacity of the rectangle to another value, your code appears to have no effect, because the animation is still affecting the Opacity property.

One way to regain control of an animated property in code is to use the BeginAnimation method and specify null for the AnimationTimeline parameter. For more information and an example, see Set a Property After Animating It with a Storyboard.

Note that, although setting a property value that has an Active or Filling animation appears to have no effect, the property value does change. For more information, see the Animation and Timing System Overview.

Data Binding and Animating Animations

Most animation properties can be data bound or animated; for example, you can animate the Duration property of a DoubleAnimation. However, because of the way the timing system works, data bound or animated animations do not behave like other data bound or animated objects. To understand their behavior, it helps to understand what it means to apply an animation to a property.

Refer to the example in the previous section that showed how to animate the Opacity of a rectangle. When the rectangle in the previous example is loaded, its event trigger applies the Storyboard. The timing system creates a copy of the Storyboard and its animation. These copies are frozen (made read-only) and Clock objects are created from them. These clocks do the actual work of animating the targeted properties.

The timing system creates a clock for the DoubleAnimation and applies it to the object and property that is specified by the TargetName and TargetProperty of the DoubleAnimation. In this case, the timing system applies the clock to the Opacity property of the object that is named “MyRectangle.”

Although a clock is also created for the Storyboard, the clock is not applied to any properties. Its purpose is to control its child clock, the clock that is created for the DoubleAnimation.

For an animation to reflect data binding or animation changes, its clock must be regenerated. Clocks are not regenerated for you automatically. To make an animation reflect changes, reapply its storyboard by using a BeginStoryboard or the Begin method. When you use either of these methods, the animation restarts. In code, you can use the Seek method to shift the storyboard back to its previous position.

For an example of a data bound animation, see Key Spline Animation Sample. For more information about how the animation and timing system works, see Animation and Timing System Overview.

Other Ways to Animate

The examples in this overview show how to animate by using storyboards. When you use code, you can animate in several other ways. For more information, see the Property Animation Techniques Overview.

Animation Samples

The following samples can help you start adding animation to your applications.

From, To, and By Animation Target Values Sample Demonstrates different From/To/By settings.

Animation Timing Behavior Sample Demonstrates the different ways you can control the timing behavior of an animation. This sample also shows how to data bind the destination value of an animation.

Related Topics

Reference

Storyboards Overview – WPF .NET Framework

Table of contents

Storyboards Overview

Article

03/17/2022

21 minutes to read

3 contributors

In this article

This topic shows how to use Storyboard objects to organize and apply animations. It describes how to interactively manipulate Storyboard objects and describes indirect property targeting syntax.

Prerequisites

To understand this topic, you should be familiar with the different animation types and their basic features. For an introduction to animation, see the Animation Overview. You should also know how to use attached properties. For more information about attached properties, see the Attached Properties Overview.

What Is a Storyboard

Animations are not the only useful type of timeline. Other timeline classes are provided to help you organize sets of timelines, and to apply timelines to properties. Container timelines derive from the TimelineGroup class, and include ParallelTimeline and Storyboard.

A Storyboard is a type of container timeline that provides targeting information for the timelines it contains. A Storyboard can contain any type of Timeline, including other container timelines and animations. Storyboard objects enable you to combine timelines that affect a variety of objects and properties into a single timeline tree, making it easy to organize and control complex timing behaviors. For example, suppose you want a button that does these three things.

Grow and change color when the user selects the button.

Shrink away and then grow back to its original size when clicked.

Shrink and fade to 50 percent opacity when it becomes disabled.

In this case, you have multiple sets of animations that apply to the same object, and you want to play at different times, dependent on the state of the button. Storyboard objects enable you to organize animations and apply them in groups to one or more objects.

Where Can You Use a Storyboard

A Storyboard can be used to animate dependency properties of animatable classes (for more information about what makes a class animatable, see the Animation Overview). However, because storyboarding is a framework-level feature, the object must belong to the NameScope of a FrameworkElement or a FrameworkContentElement.

For example, you could use a Storyboard to do the following:

Animate a SolidColorBrush (Non-framework element) that paints the Background of a Button (a type of FrameworkElement),

Animate a SolidColorBrush (Non-framework element) that paints the fill of a GeometryDrawing (Non-framework element) displayed using an Image (FrameworkElement).

In code, animate a SolidColorBrush declared by a class that also contains a FrameworkElement, if the SolidColorBrush registered its name with that FrameworkElement.

However, you could not use a Storyboard to animate a SolidColorBrush that did not register its name with a FrameworkElement or FrameworkContentElement, or was not used to set a property of a FrameworkElement or FrameworkContentElement.

How to Apply Animations with a Storyboard

To use a Storyboard to organize and apply animations, you add the animations as child timelines of the Storyboard. 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. In XAML, you use a BeginStoryboard object with an EventTrigger, Trigger, or DataTrigger. In code, you can also use the Begin method.

The following table shows the different places where each Storyboard begin technique is supported: per-instance, style, control template, and data template. “Per-Instance” refers to the technique of applying an animation or storyboard directly to instances of an object, rather than in a style, control template, or data template.

The following example uses a Storyboard to animate the Width of a Rectangle element and the Color of a SolidColorBrush used to paint that Rectangle.

using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Data; using System.Windows.Shapes; using System.Windows.Input; namespace Microsoft.Samples.Animation { public class StoryboardsExample : Page { public StoryboardsExample() { this.WindowTitle = “Storyboards Example”; StackPanel myStackPanel = new StackPanel(); myStackPanel.Margin = new Thickness(20); Rectangle myRectangle = new Rectangle(); myRectangle.Name = “MyRectangle”; // Create a name scope for the page. NameScope.SetNameScope(this, new NameScope()); this.RegisterName(myRectangle.Name, myRectangle); myRectangle.Width = 100; myRectangle.Height = 100; SolidColorBrush mySolidColorBrush = new SolidColorBrush(Colors.Blue); this.RegisterName(“MySolidColorBrush”, mySolidColorBrush); myRectangle.Fill = mySolidColorBrush; DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 100; myDoubleAnimation.To = 200; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty)); ColorAnimation myColorAnimation = new ColorAnimation(); myColorAnimation.From = Colors.Blue; myColorAnimation.To = Colors.Red; myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myColorAnimation, “MySolidColorBrush”); Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); myStoryboard.Children.Add(myColorAnimation); myRectangle.MouseEnter += delegate(object sender, MouseEventArgs e) { myStoryboard.Begin(this); }; myStackPanel.Children.Add(myRectangle); this.Content = myStackPanel; } } }

The following sections describe the TargetName and TargetProperty attached properties in more detail.

Targeting Framework Elements, Framework Content Elements, and Freezables

The previous section mentioned that, for an animation to find its target, it must know the target’s name and the property to animate. Specifying the property to animate is straight forward: simply set TargetProperty with the name of the property to animate. You specify the name of the object whose property you want to animate by setting the Storyboard.TargetName property on the animation.

Caution While you can use the Target property to bind directly to an object as an alternative to TargetName , it isn’t serializable. There is no guaranteed that the Target object can be correctly referenced in XAML.

For the TargetName property to work, the targeted object must have a name. Assigning a name to a FrameworkElement or a FrameworkContentElement in XAML is different than assigning a name to a Freezable object.

Framework elements are those classes that inherit from the FrameworkElement class. Examples of framework elements include Window, DockPanel, Button, and Rectangle. Essentially all windows, panels, and controls are elements. Framework content elements are those classes that inherit from the FrameworkContentElement class. Examples of framework content elements include FlowDocument and Paragraph. If you’re not sure whether a type is a framework element or a framework content element, check to see whether it has a Name property. If it does, it’s probably a framework element or a framework content element. To be sure, check the Inheritance Hierarchy section of its type page.

To enable the targeting of a framework element or a framework content element in XAML, you set its Name property. In code, you also need to use the RegisterName method to register the element’s name with the element for which you’ve created a NameScope.

The following example, taken from the preceding example, assigns the name MyRectangle a Rectangle, a type of FrameworkElement.

Rectangle myRectangle = new Rectangle(); myRectangle.Name = “MyRectangle”; // Create a name scope for the page. NameScope.SetNameScope(this, new NameScope()); this.RegisterName(myRectangle.Name, myRectangle);

After it has a name, you can animate a property of that element.

Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));

Freezable types are those classes that inherit from the Freezable class. Examples of Freezable include SolidColorBrush, RotateTransform, and GradientStop.

To enable the targeting of a Freezable by an animation in XAML, you use the x:Name Directive to assign it a name. In code, you use the RegisterName method to register its name with the element for which you’ve created a NameScope.

The following example assigns a name to a Freezable object.

SolidColorBrush mySolidColorBrush = new SolidColorBrush(Colors.Blue); this.RegisterName(“MySolidColorBrush”, mySolidColorBrush);

The object can then be targeted by an animation.

Storyboard.SetTargetName(myColorAnimation, “MySolidColorBrush”); Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));

Storyboard objects use name scopes to resolve the TargetName property. For more information about WPF name scopes, see WPF XAML Namescopes. If the TargetName property is omitted, the animation targets the element on which it is defined, or, in the case of styles, the styled element.

Sometimes a name can’t be assigned to a Freezable object. For example, if a Freezable is declared as a resource or used to set a property value in a style, it can’t be given a name. Because it doesn’t have a name, it can’t be targeted directly—but it can be targeted indirectly. The following sections describe how to use indirect targeting.

Indirect Targeting

There are times a Freezable can’t be targeted directly by an animation, such as when the Freezable is declared as a resource or used to set a property value in a style. In these cases, even though you can’t target it directly, you can still animate the Freezable object. Instead of setting the TargetName property with the name of the Freezable, you give it the name of the element to which the Freezable “belongs.” For example, a SolidColorBrush used to set the Fill of a rectangle element belongs to that rectangle. To animate the brush, you would set the animation’s TargetProperty with a chain of properties that starts at the property of the framework element or framework content element the Freezable was used to set and ends with the Freezable property to animate.

DependencyProperty[] propertyChain = new DependencyProperty[] {Rectangle.FillProperty, SolidColorBrush.ColorProperty}; string thePath = “(0).(1)”; PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); Storyboard.SetTargetProperty(myColorAnimation, myPropertyPath);

Note that, if the Freezable is frozen, a clone will be made, and that clone will be animated. When this happens, the original object’s HasAnimatedProperties property continues to return false , because the original object is not actually animated. For more information about cloning, see the Freezable Objects Overview.

Also note that, when using indirect property targeting, it’s possible to target objects that don’t exist. For example, you might assume that the Background of a particular button was set with a SolidColorBrush and try to animate its Color, when in fact a LinearGradientBrush was used to set the button’s Background. In these cases, no exception is thrown; the animation fails to have a visible effect because LinearGradientBrush does not react to changes to the Color property.

The following sections describe indirect property targeting syntax in more detail.

Indirectly Targeting a Property of a Freezable in XAML

To target a property of a freezable in XAML, use the following syntax.

Property syntax ElementPropertyName . FreezablePropertyName

Where

ElementPropertyName is the property of the FrameworkElement which the Freezable is used to set, and

FreezablePropertyName is the property of the Freezable to animate.

The following code shows how to animate the Color of a SolidColorBrush used to set the Fill of a rectangle element.

Sometimes you need to target a freezable contained in a collection or array.

To target a freezable contained in a collection, you use the following path syntax.

Path syntax ElementPropertyName .Children[ CollectionIndex ]. FreezablePropertyName

Where CollectionIndex is the index of the object in its array or collection.

For example, suppose that a rectangle has a TransformGroup resource applied to its RenderTransform property, and you want to animate one of the transforms it contains.

The following code shows how to animate the Angle property of the RotateTransform shown in the previous example.

Indirectly Targeting a Property of a Freezable in Code

In code, you create a PropertyPath object. When you create the PropertyPath, you specify a Path and PathParameters.

To create PathParameters, you create an array of type DependencyProperty that contains a list of dependency property identifier fields. The first identifier field is for the property of the FrameworkElement or FrameworkContentElement that the Freezable is used to set. The next identifier field represents the property of the Freezable to target. Think of it as a chain of properties that connects the Freezable to the FrameworkElement object.

The following is an example of a dependency property chain that targets the Color of a SolidColorBrush used to set the Fill of a rectangle element.

DependencyProperty[] propertyChain = new DependencyProperty[] {Rectangle.FillProperty, SolidColorBrush.ColorProperty};

You also need to specify a Path. A Path is a String that tells the Path how to interpret its PathParameters. It uses the following syntax.

Property path syntax ( OwnerPropertyArrayIndex ).( FreezablePropertyArrayIndex )

Where

OwnerPropertyArrayIndex is the index of the DependencyProperty array that contains the identifier of the FrameworkElement object’s property that the Freezable is used to set, and

FreezablePropertyArrayIndex is the index of the DependencyProperty array that contains the identifier of property to target.

The following example shows the Path that would accompany the PathParameters defined in the preceding example.

DependencyProperty[] propertyChain = new DependencyProperty[] {Rectangle.FillProperty, SolidColorBrush.ColorProperty}; string thePath = “(0).(1)”;

The following example combines the code in the previous examples to animate the Color of a SolidColorBrush used to set the Fill of a rectangle element.

// Create a name scope for the page. NameScope.SetNameScope(this, new NameScope()); Rectangle rectangle01 = new Rectangle(); rectangle01.Name = “Rectangle01”; this.RegisterName(rectangle01.Name, rectangle01); rectangle01.Width = 100; rectangle01.Height = 100; rectangle01.Fill = (SolidColorBrush)this.Resources[“MySolidColorBrushResource”]; ColorAnimation myColorAnimation = new ColorAnimation(); myColorAnimation.From = Colors.Blue; myColorAnimation.To = Colors.AliceBlue; myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myColorAnimation, rectangle01.Name); DependencyProperty[] propertyChain = new DependencyProperty[] {Rectangle.FillProperty, SolidColorBrush.ColorProperty}; string thePath = “(0).(1)”; PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); Storyboard.SetTargetProperty(myColorAnimation, myPropertyPath); Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myColorAnimation); BeginStoryboard myBeginStoryboard = new BeginStoryboard(); myBeginStoryboard.Storyboard = myStoryboard; EventTrigger myMouseEnterTrigger = new EventTrigger(); myMouseEnterTrigger.RoutedEvent = Rectangle.MouseEnterEvent; myMouseEnterTrigger.Actions.Add(myBeginStoryboard); rectangle01.Triggers.Add(myMouseEnterTrigger);

Sometimes you need to target a freezable contained in a collection or array. For example, suppose that a rectangle has a TransformGroup resource applied to its RenderTransform property, and you want to animate one of the transforms it contains.

To target a Freezable contained in a collection, you use the following path syntax.

Path syntax ( OwnerPropertyArrayIndex ).( CollectionChildrenPropertyArrayIndex ) [ CollectionIndex ].( FreezablePropertyArrayIndex )

Where CollectionIndex is the index of the object in its array or collection.

To target the Angle property of the RotateTransform, the second transform in the TransformGroup, you would use the following Path and PathParameters.

DependencyProperty[] propertyChain = new DependencyProperty[] { Rectangle.RenderTransformProperty, TransformGroup.ChildrenProperty, RotateTransform.AngleProperty }; string thePath = “(0).(1)[1].(2)”; PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); Storyboard.SetTargetProperty(myDoubleAnimation, myPropertyPath);

The following example shows the complete code for animating the Angle of a RotateTransform contained within a TransformGroup.

Rectangle rectangle02 = new Rectangle(); rectangle02.Name = “Rectangle02”; this.RegisterName(rectangle02.Name, rectangle02); rectangle02.Width = 100; rectangle02.Height = 100; rectangle02.Fill = Brushes.Blue; rectangle02.RenderTransform = (TransformGroup)this.Resources[“MyTransformGroupResource”]; DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 0; myDoubleAnimation.To = 360; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myDoubleAnimation, rectangle02.Name); DependencyProperty[] propertyChain = new DependencyProperty[] { Rectangle.RenderTransformProperty, TransformGroup.ChildrenProperty, RotateTransform.AngleProperty }; string thePath = “(0).(1)[1].(2)”; PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); Storyboard.SetTargetProperty(myDoubleAnimation, myPropertyPath); Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); BeginStoryboard myBeginStoryboard = new BeginStoryboard(); myBeginStoryboard.Storyboard = myStoryboard; EventTrigger myMouseEnterTrigger = new EventTrigger(); myMouseEnterTrigger.RoutedEvent = Rectangle.MouseEnterEvent; myMouseEnterTrigger.Actions.Add(myBeginStoryboard); rectangle02.Triggers.Add(myMouseEnterTrigger);

Indirectly Targeting with a Freezable as the Starting Point

The previous sections described how to indirectly target a Freezable by starting with a FrameworkElement or FrameworkContentElement and creating a property chain to a Freezable sub-property. You can also use a Freezable as a starting point and indirectly target one of its Freezable sub-properties. One additional restriction applies when using a Freezable as a starting point for indirect targeting: the starting Freezable and every Freezable between it and the indirectly targeted sub-property must not be frozen.

Interactively Controlling a Storyboard in XAML

To start a storyboard in Extensible Application Markup Language (XAML), you use a BeginStoryboard trigger action. BeginStoryboard distributes the animations to the objects and properties they animate, and starts the storyboard. (For details about this process, see the Animation and Timing System Overview.) If you give the BeginStoryboard a name by specifying its Name property, you make it a controllable storyboard. You can then interactively control the storyboard after it’s started. The following is a list of controllable storyboard actions that you use with event triggers to control a storyboard.

PauseStoryboard: Pauses the storyboard.

ResumeStoryboard: Resumes a paused storyboard.

SetStoryboardSpeedRatio: Changes the storyboard’s speed.

SkipStoryboardToFill: Advances a storyboard to the end of its fill period, if it has one.

StopStoryboard: Stops the storyboard.

RemoveStoryboard: Removes the storyboard.

In the following example, controllable storyboard actions are used to interactively control a storyboard.

Interactively Controlling a Storyboard by Using Code

The previous examples have shown how to animate using trigger actions. In code, you may also control a storyboard using interactive methods of the Storyboard class. For a Storyboard to be made interactive in code, you must use the appropriate overload of the storyboard’s Begin method and specify true to make it controllable. See the Begin(FrameworkElement, Boolean) page for more information.

The following list shows the methods that can be used to manipulate a Storyboard after it has started:

The advantage to using these methods is that you don’t need to create Trigger or TriggerAction objects; you just need a reference to the controllable Storyboard you want to manipulate.

Note All interactive actions taken on a Clock, and therefore also on a Storyboard will occur on the next tick of the timing engine which will happen shortly before the next render. For example, if you use the Seek method to jump to another point in an animation, the property value does not change instantly, rather, the value changes on the next tick of the timing engine.

The following example shows how to apply and control animations using the interactive methods of the Storyboard class.

using System; using System.Windows; using System.Windows.Controls; using System.Windows.Shapes; using System.Windows.Media; using System.Windows.Media.Animation; namespace SDKSample { public class ControllableStoryboardExample : Page { private Storyboard myStoryboard; public ControllableStoryboardExample() { // Create a name scope for the page. NameScope.SetNameScope(this, new NameScope()); this.WindowTitle = “Controllable Storyboard Example”; StackPanel myStackPanel = new StackPanel(); myStackPanel.Margin = new Thickness(10); // Create a rectangle. Rectangle myRectangle = new Rectangle(); myRectangle.Name = “myRectangle”; // Assign the rectangle a name by // registering it with the page, so that // it can be targeted by storyboard // animations. this.RegisterName(myRectangle.Name, myRectangle); myRectangle.Width = 100; myRectangle.Height = 100; myRectangle.Fill = Brushes.Blue; myStackPanel.Children.Add(myRectangle); // // Create an animation and a storyboard to animate the // rectangle. // DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 1.0; myDoubleAnimation.To = 0.0; myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(5000)); myDoubleAnimation.AutoReverse = true; // Create the storyboard. myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty)); // // Create some buttons to control the storyboard // and a panel to contain them. // StackPanel buttonPanel = new StackPanel(); buttonPanel.Orientation = Orientation.Horizontal; Button beginButton = new Button(); beginButton.Content = “Begin”; beginButton.Click += new RoutedEventHandler(beginButton_Clicked); buttonPanel.Children.Add(beginButton); Button pauseButton = new Button(); pauseButton.Content = “Pause”; pauseButton.Click += new RoutedEventHandler(pauseButton_Clicked); buttonPanel.Children.Add(pauseButton); Button resumeButton = new Button(); resumeButton.Content = “Resume”; resumeButton.Click += new RoutedEventHandler(resumeButton_Clicked); buttonPanel.Children.Add(resumeButton); Button skipToFillButton = new Button(); skipToFillButton.Content = “Skip to Fill”; skipToFillButton.Click += new RoutedEventHandler(skipToFillButton_Clicked); buttonPanel.Children.Add(skipToFillButton); Button setSpeedRatioButton = new Button(); setSpeedRatioButton.Content = “Triple Speed”; setSpeedRatioButton.Click += new RoutedEventHandler(setSpeedRatioButton_Clicked); buttonPanel.Children.Add(setSpeedRatioButton); Button stopButton = new Button(); stopButton.Content = “Stop”; stopButton.Click += new RoutedEventHandler(stopButton_Clicked); buttonPanel.Children.Add(stopButton); myStackPanel.Children.Add(buttonPanel); this.Content = myStackPanel; } // Begins the storyboard. private void beginButton_Clicked(object sender, RoutedEventArgs args) { // Specifying “true” as the second Begin parameter // makes this storyboard controllable. myStoryboard.Begin(this, true); } // Pauses the storyboard. private void pauseButton_Clicked(object sender, RoutedEventArgs args) { myStoryboard.Pause(this); } // Resumes the storyboard. private void resumeButton_Clicked(object sender, RoutedEventArgs args) { myStoryboard.Resume(this); } // Advances the storyboard to its fill period. private void skipToFillButton_Clicked(object sender, RoutedEventArgs args) { myStoryboard.SkipToFill(this); } // Updates the storyboard’s speed. private void setSpeedRatioButton_Clicked(object sender, RoutedEventArgs args) { // Makes the storyboard progress three times as fast as normal. myStoryboard.SetSpeedRatio(this, 3); } // Stops the storyboard. private void stopButton_Clicked(object sender, RoutedEventArgs args) { myStoryboard.Stop(this); } } }

Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Shapes Imports System.Windows.Media Imports System.Windows.Media.Animation Namespace SDKSample Public Class ControllableStoryboardExample Inherits Page Private myStoryboard As Storyboard Public Sub New() ‘ Create a name scope for the page. NameScope.SetNameScope(Me, New NameScope()) Me.WindowTitle = “Controllable Storyboard Example” Dim myStackPanel As New StackPanel() myStackPanel.Margin = New Thickness(10) ‘ Create a rectangle. Dim myRectangle As New Rectangle() myRectangle.Name = “myRectangle” ‘ Assign the rectangle a name by ‘ registering it with the page, so that ‘ it can be targeted by storyboard ‘ animations. Me.RegisterName(myRectangle.Name, myRectangle) myRectangle.Width = 100 myRectangle.Height = 100 myRectangle.Fill = Brushes.Blue myStackPanel.Children.Add(myRectangle) ‘ ‘ Create an animation and a storyboard to animate the ‘ rectangle. ‘ Dim myDoubleAnimation As New DoubleAnimation() myDoubleAnimation.From = 1.0 myDoubleAnimation.To = 0.0 myDoubleAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(5000)) myDoubleAnimation.AutoReverse = True ‘ Create the storyboard. myStoryboard = New Storyboard() myStoryboard.Children.Add(myDoubleAnimation) Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name) Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.OpacityProperty)) ‘ ‘ Create some buttons to control the storyboard ‘ and a panel to contain them. ‘ Dim buttonPanel As New StackPanel() buttonPanel.Orientation = Orientation.Horizontal Dim beginButton As New Button() beginButton.Content = “Begin” AddHandler beginButton.Click, AddressOf beginButton_Clicked buttonPanel.Children.Add(beginButton) Dim pauseButton As New Button() pauseButton.Content = “Pause” AddHandler pauseButton.Click, AddressOf pauseButton_Clicked buttonPanel.Children.Add(pauseButton) Dim resumeButton As New Button() resumeButton.Content = “Resume” AddHandler resumeButton.Click, AddressOf resumeButton_Clicked buttonPanel.Children.Add(resumeButton) Dim skipToFillButton As New Button() skipToFillButton.Content = “Skip to Fill” AddHandler skipToFillButton.Click, AddressOf skipToFillButton_Clicked buttonPanel.Children.Add(skipToFillButton) Dim setSpeedRatioButton As New Button() setSpeedRatioButton.Content = “Triple Speed” AddHandler setSpeedRatioButton.Click, AddressOf setSpeedRatioButton_Clicked buttonPanel.Children.Add(setSpeedRatioButton) Dim stopButton As New Button() stopButton.Content = “Stop” AddHandler stopButton.Click, AddressOf stopButton_Clicked buttonPanel.Children.Add(stopButton) myStackPanel.Children.Add(buttonPanel) Me.Content = myStackPanel End Sub ‘ Begins the storyboard. Private Sub beginButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs) ‘ Specifying “true” as the second Begin parameter ‘ makes this storyboard controllable. myStoryboard.Begin(Me, True) End Sub ‘ Pauses the storyboard. Private Sub pauseButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs) myStoryboard.Pause(Me) End Sub ‘ Resumes the storyboard. Private Sub resumeButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs) myStoryboard.Resume(Me) End Sub ‘ Advances the storyboard to its fill period. Private Sub skipToFillButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs) myStoryboard.SkipToFill(Me) End Sub ‘ Updates the storyboard’s speed. Private Sub setSpeedRatioButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs) ‘ Makes the storyboard progress three times as fast as normal. myStoryboard.SetSpeedRatio(Me, 3) End Sub ‘ Stops the storyboard. Private Sub stopButton_Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs) myStoryboard.Stop(Me) End Sub End Class End Namespace

Animate in a Style

You can use Storyboard objects to define animations in a Style. Animating with a Storyboard in a Style is similar to using a Storyboard elsewhere, with the following three exceptions:

You don’t specify a TargetName; the Storyboard always targets the element to which the Style is applied. To target Freezable objects, you must use indirect targeting. For more information about indirect targeting, see the Indirect Targeting section.

You can’t specify a SourceName for an EventTrigger or a Trigger.

You can’t use dynamic resource references or data binding expressions to set Storyboard or animation property values. That’s because everything inside a Style must be thread-safe, and the timing system must FreezeStoryboard objects to make them thread-safe. A Storyboard cannot be frozen if it or its child timelines contain dynamic resource references or data binding expressions. For more information about freezing and other Freezable features, see the Freezable Objects Overview.

In XAML, you can’t declare event handlers for Storyboard or animation events.

For an example showing how to define a storyboard in a style, see the Animate in a Style example.

Animate in a ControlTemplate

You can use Storyboard objects to define animations in a ControlTemplate. Animating with a Storyboard in a ControlTemplate is similar to using a Storyboard elsewhere, with the following two exceptions:

The TargetName may only refer to child objects of the ControlTemplate. If TargetName is not specified, the animation targets the element to which the ControlTemplate is applied.

The SourceName for an EventTrigger or a Trigger may only refer to child objects of the ControlTemplate.

You can’t use dynamic resource references or data binding expressions to set Storyboard or animation property values. That’s because everything inside a ControlTemplate must be thread-safe, and the timing system must FreezeStoryboard objects to make them thread-safe. A Storyboard cannot be frozen if it or its child timelines contain dynamic resource references or data binding expressions. For more information about freezing and other Freezable features, see the Freezable Objects Overview.

In XAML, you can’t declare event handlers for Storyboard or animation events.

For an example showing how to define a storyboard in a ControlTemplate, see the Animate in a ControlTemplate example.

Animate When a Property Value Changes

In styles and control templates, you can use Trigger objects to start a storyboard when a property changes. For examples, see Trigger an Animation When a Property Value Changes and Animate in a ControlTemplate.

Animations applied by property Trigger objects behave in a more complex fashion than EventTrigger animations or animations started using Storyboard methods. They “handoff” with animations defined by other Trigger objects, but compose with EventTrigger and method-triggered animations.

See also

So you have finished reading the wpf doubleanimation topic article, if you find this article useful, please share it. Thank you very much. See more: Storyboard template wpf, Storyboard WPF, WPF animation, WPF ScaleTransform animation, Gif WPF, wpf storyboard, Loading screen wpf, WPF sample

Leave a Comment