Storyboard Storyboard Storyboard Class

Definition

Controls animations with a timeline, and provides object and property targeting information for its child animations.

public sealed class Storyboard : Timeline, IStoryboardpublic sealed class Storyboard : Timeline, IStoryboardPublic NotInheritable Class Storyboard Inherits Timeline Implements IStoryboard
<Storyboard ...>
  oneOrMoreChildTimelines
</Storyboard>
Inheritance
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Inherited Members

Inherited properties

Inherited methods

Inherited events

Remarks

Storyboard is an important class in the concept of storyboarded animations. For more info on the concept, see Storyboarded animations.

Storyboard is used for these properties:

These properties aren't the only place where a Storyboard is defined. The typical way that a Storyboard is used for a storyboarded animation is that the Storyboard is defined in a Resources collection (either Application.Resources or FrameworkElement.Resources, or possibly as a resource within a file such as Generic.xaml for a custom control). Whenever it's defined as a XAML resource, you should always assign an x:Name attribute value to your Storyboard. You can then reference the name as a programming variable later in code-behind. You'll need this reference to actually run the animations that the Storyboard contains, by calling the Begin method on that Storyboard instance. Storyboard also has other control methods such as Stop that can control the animation thereafter.

Storyboard inherits several properties from Timeline. These properties can be applied either to a Storyboard or to one of the animations within it (in the Children collection). There are pros and cons to setting Timeline properties on the main Storyboard instead of on each animation. For more info, see Storyboarded animations.

You also need a Storyboard in order to control the predefined animations that you add to controls or UI, if you are using one of the theme animations. Theme animations don't have an innate trigger point, so you need to contain theme animations in a Storyboard as the Children. If the Storyboard is used as the VisualState.Storyboard value then the animation runs when that visual state is loaded. Or, if it's in a VisualTransition.Storyboard, the animation runs when that transition is detected by the visual state manager. These are the most common way to use a theme animation, but you could also put one in a loose Storyboard resource and explicitly start the animation by calling Begin. For more info on how to use theme animations, see Quickstart: Animating your UI using library animations or Storyboarded animations for visual states.

Storyboard XAML attached properties

Storyboard is the host service class for several XAML attached properties. Theses enable child animations under control by the Storyboard to each target separate target elements and target properties, while still following the same controlling timeline and triggering mechanism as the parent.

The XAML attached properties that are supported by Storyboard are:

  • Storyboard.TargetName
  • Storyboard.TargetProperty Storyboard.TargetName is used to reference another element by its name. The element being referenced is the element/object where the animation should apply. This mechanism is part of the basic design of the animation system: it enables animation resources to be declared separately from UI declaration resources, and makes it possible to apply one animation definition to several different property behaviors. For the value of Storyboard.TargetName on a particular animation, you specify the target element's Name or x:Name attribute value, which is a string. That named element should already exist in some other area of XAML markup.

The meaning of Name /x:Name attribute strings is controlled by a XAML namescope concept. For most animation targeting scenarios you won't need to worry about the influence of XAML namescopes, but you might encounter XAML name resolution issues if you're trying to target template parts, or objects that were created using XamlReader.Load and subsequently added to the object tree. For more info, see XAML namescopes.

Storyboard.TargetProperty targets a particular property of the element as named by Storyboard.TargetName. The value you provide for Storyboard.TargetProperty involves a concept known as a property path. For more information on how to specify a property path for an animation, see Remarks in SetTargetProperty or Storyboard.TargetProperty, or the Property-path syntax topic.

In order to support XAML processor access to the attached properties, and also to expose equivalent get and set operations to code, each XAML attached property has a pair of Get and Set accessor methods. For example, the GetTargetName and SetTargetName methods support and provide the equivalent code-only support for Storyboard.TargetName. Alternatively, you can use the dependency property system to get or set the value of the attached property. Call GetValue or SetValue, passing the arguments of the dependency property identifier to set, and a reference to the target object on which to get or set the value.

Examples

The following example shows how to use the Begin, Stop, Pause, and Resume methods to control the playback of a storyboard (animation). A set of buttons allow the user to call these methods.

<StackPanel x:Name="LayoutRoot" >
    <StackPanel.Resources>
        <Storyboard x:Name="myStoryboard">
            <DoubleAnimation From="1" To="6" Duration="00:00:6" 
            Storyboard.TargetName="rectScaleTransform" 
            Storyboard.TargetProperty="ScaleY">
                <DoubleAnimation.EasingFunction>
                    <BounceEase Bounces="2" EasingMode="EaseOut" 
                            Bounciness="2" />
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
    </StackPanel.Resources>

    <!-- Button that begins animation. -->
    <Button Click="Animation_Begin"
         Margin="2" Content="Begin" />

    <!-- Button that pauses Animation. -->
    <Button Click="Animation_Pause"
         Margin="2" Content="Pause" />

    <!-- Button that resumes Animation. -->
    <Button Click="Animation_Resume"
         Margin="2" Content="Resume" />

    <!-- Button that stops Animation. Stopping the animation 
         returns the ellipse to its original location. -->
    <Button Click="Animation_Stop"
         Margin="2" Content="Stop" />

    <Rectangle Fill="Blue" Width="200" Height="30">
        <Rectangle.RenderTransform>
            <ScaleTransform x:Name="rectScaleTransform" />
        </Rectangle.RenderTransform>
    </Rectangle>

</StackPanel>
private void Animation_Begin(object sender, RoutedEventArgs e)
{
    myStoryboard.Begin();
}
private void Animation_Pause(object sender, RoutedEventArgs e)
{
    myStoryboard.Pause();
}
private void Animation_Resume(object sender, RoutedEventArgs e)
{
    myStoryboard.Resume();
}
private void Animation_Stop(object sender, RoutedEventArgs e)
{
    myStoryboard.Stop();
}
Private Sub Animation_Begin(sender As Object, e As RoutedEventArgs)
    myStoryboard.Begin()
End Sub

Private Sub Animation_Pause(sender As Object, e As RoutedEventArgs)
    myStoryboard.Pause()
End Sub

Private Sub Animation_Resume(sender As Object, e As RoutedEventArgs)
    myStoryboard.Resume()
End Sub

Private Sub Animation_Stop(sender As Object, e As RoutedEventArgs)
    myStoryboard.Stop()
End Sub
        //using Windows.UI.Xaml.Media.Animation;
        //using Windows.UI.Xaml.Shapes;
        //using Windows.UI

        private void Create_And_Run_Animation(object sender, RoutedEventArgs e)
        {
            // Create a red rectangle that will be the target
            // of the animation.
            Rectangle myRectangle = new Rectangle();
            myRectangle.Width = 200;
            myRectangle.Height = 200;
            SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
            myRectangle.Fill = myBrush;

            // Create the transform
            TranslateTransform moveTransform = new TranslateTransform();
            moveTransform.X = 0;
            moveTransform.Y = 0;
            myRectangle.RenderTransform = moveTransform;

            // Add the rectangle to the tree.
            LayoutRoot.Children.Add(myRectangle);

            // Create a duration of 2 seconds.
            Duration duration = new Duration(TimeSpan.FromSeconds(2));
            // Create two DoubleAnimations and set their properties.
            DoubleAnimation myDoubleAnimationX = new DoubleAnimation();
            DoubleAnimation myDoubleAnimationY = new DoubleAnimation();
            myDoubleAnimationX.Duration = duration;
            myDoubleAnimationY.Duration = duration;
            Storyboard justintimeStoryboard = new Storyboard();
            justintimeStoryboard.Duration = duration;
            justintimeStoryboard.Children.Add(myDoubleAnimationX);
            justintimeStoryboard.Children.Add(myDoubleAnimationY);
            Storyboard.SetTarget(myDoubleAnimationX, moveTransform);
            Storyboard.SetTarget(myDoubleAnimationY, moveTransform);

            // Set the X and Y properties of the Transform to be the target properties
            // of the two respective DoubleAnimations.
            Storyboard.SetTargetProperty(myDoubleAnimationX, "X");
            Storyboard.SetTargetProperty(myDoubleAnimationY, "Y");
            myDoubleAnimationX.To = 200;
            myDoubleAnimationY.To = 200;

            // Make the Storyboard a resource.
            LayoutRoot.Resources.Add("justintimeStoryboard", justintimeStoryboard);
            // Begin the animation.
            justintimeStoryboard.Begin();
        }
    ' need Imports for Windows.UI.Xaml.Shapes, Windows.UI.Media.Animation, Windows.UI
    Private Sub Create_And_Run_Animation(sender As Object, e As RoutedEventArgs)
        ' Create a red rectangle that will be the target
        ' of the animation.
        Dim myRectangle As Rectangle = New Rectangle
        myRectangle.Width = 200
        myRectangle.Height = 200
        Dim myBrush As SolidColorBrush = New SolidColorBrush(Colors.Red)
        myRectangle.Fill = myBrush

       ' Create the transform
        Dim moveTransform As TranslateTransform = New TranslateTransform
        moveTransform.X = 0
        moveTransform.Y = 0
        myRectangle.RenderTransform = moveTransform

        ' Add the rectangle to the tree.
        LayoutRoot.Children.Add(myRectangle)

        ' Create a duration of 2 seconds.
        Dim duration As Duration = New Duration(TimeSpan.FromSeconds(2))
        ' Create two DoubleAnimations and set their properties.
        Dim myDoubleAnimationX As DoubleAnimation = New DoubleAnimation
        Dim myDoubleAnimationY As DoubleAnimation = New DoubleAnimation
        myDoubleAnimationX.Duration = duration
        myDoubleAnimationY.Duration = duration
        Dim justintimeStoryboard As Storyboard = New Storyboard
        justintimeStoryboard.Duration = duration
        justintimeStoryboard.Children.Add(myDoubleAnimationX)
        justintimeStoryboard.Children.Add(myDoubleAnimationY)
        Storyboard.SetTarget(myDoubleAnimationX, moveTransform)
        Storyboard.SetTarget(myDoubleAnimationY, moveTransform)

        ' Set the X and Y properties of the Transform to be the target properties
        ' of the two respective DoubleAnimations.
        Storyboard.SetTargetProperty(myDoubleAnimationX, "X")
        Storyboard.SetTargetProperty(myDoubleAnimationY, "Y")
        myDoubleAnimationX.To = 200
        myDoubleAnimationY.To = 200

        ' Make the Storyboard a resource.
        LayoutRoot.Resources.Add("justintimeStoryboard", justintimeStoryboard)
        ' Begin the animation.
        justintimeStoryboard.Begin()
    End Sub

Constructors

Storyboard() Storyboard() Storyboard()

Initializes a new instance of the Storyboard class.

public Storyboard()public Storyboard()Public Sub New()
Attributes

Properties

Children Children Children

Gets the collection of child Timeline objects.

public TimelineCollection Children { get; }public TimelineCollection Children { get; }Public ReadOnly Property Children As TimelineCollection
<Storyboard ...>
  oneOrMoreChildTimelines
</Storyboard>
Value
TimelineCollection TimelineCollection TimelineCollection

The collection of child Timeline objects. The default is an empty collection.

Attributes
See Also

TargetNameProperty TargetNameProperty TargetNameProperty

Identifies the Storyboard.TargetName XAML attached property.

public static DependencyProperty TargetNameProperty { get; }public static DependencyProperty TargetNameProperty { get; }Public Static ReadOnly Property TargetNameProperty As DependencyProperty
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the Storyboard.TargetName XAML attached property.

Attributes

TargetPropertyProperty TargetPropertyProperty TargetPropertyProperty

Identifies the Storyboard.TargetProperty XAML attached property.

public static DependencyProperty TargetPropertyProperty { get; }public static DependencyProperty TargetPropertyProperty { get; }Public Static ReadOnly Property TargetPropertyProperty As DependencyProperty
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the Storyboard.TargetProperty XAML attached property.

Attributes

Methods

Begin() Begin() Begin()

Initiates the set of animations associated with the storyboard.

public void Begin()public void Begin()Public Function Begin() As void
Attributes

Remarks

Calling this method begins all the animations that are in the Children collection of the Storyboard.

Begin starts the animations within the Storyboard from their initial state, a clock time of zero. If you want to restart an animation where Pause had been called previously, and continue on from the clock time where it was paused, use Resume instead.

See Also

GetCurrentState() GetCurrentState() GetCurrentState()

Gets the clock state of the Storyboard.

public ClockState GetCurrentState()public ClockState GetCurrentState()Public Function GetCurrentState() As ClockState
Returns

One of the enumeration values. Can be: Active, Filling, or Stopped.

Attributes

Remarks

This method is useful if you want to modify an animation's properties at runtime, before the animation is started. An animation that is stopped (GetCurrentState returns ClockState.Stopped) can still be modified. An animation that is running (GetCurrentState returns either ClockState.Active or ClockState.Filling) cannot be modified, and you'll get an error if you try.

Windows 8 behavior

An animation can have an optional BeginTime value, which modifies when the animation actually starts to affect values compared to when the controlling storyboard is started. Even if the running time has not reached the BeginTime yet, it's illegal to modify the properties of the animation once the storyboard starts it. For Windows 8, calling GetCurrentState for a running animation where the running time has not reached the BeginTime always returns ClockState.Stopped. Normally, it's legal to modify a Stopped animation's properties, but in this particular case it's not, so using GetCurrentState to determine whether it's legal to modify the animation is not always accurate. Starting with Windows 8.1, calling GetCurrentState for an animation that's been started by its storyboard always returns ClockState.Active, even if the BeginTime value exists and hasn't been reached. The circumstances in which GetCurrentState returns ClockState.Filling remain the same.

Apps that were compiled for Windows 8 but running on Windows 8.1 continue to use the Windows 8 behavior.

See Also

GetCurrentTime() GetCurrentTime() GetCurrentTime()

Gets the current animation clock time of the Storyboard.

public TimeSpan GetCurrentTime()public TimeSpan GetCurrentTime()Public Function GetCurrentTime() As TimeSpan
Returns

The current animation time of the Storyboard per the running animation clock, or null if the animation clock is Stopped.

Attributes
See Also

GetTargetName(Timeline) GetTargetName(Timeline) GetTargetName(Timeline)

Gets the value of the Storyboard.TargetName XAML attached property from a target element.

public static PlatForm::String GetTargetName(Timeline element)public static string GetTargetName(Timeline element)Public Static Function GetTargetName(element As Timeline) As string
Parameters
element
Timeline Timeline Timeline

The target element from which to get the value.

Returns
string string string

The Storyboard.TargetName value of the target element.

Attributes

GetTargetProperty(Timeline) GetTargetProperty(Timeline) GetTargetProperty(Timeline)

Gets the value of the Storyboard.TargetProperty XAML attached property from a target element.

public static PlatForm::String GetTargetProperty(Timeline element)public static string GetTargetProperty(Timeline element)Public Static Function GetTargetProperty(element As Timeline) As string
Parameters
element
Timeline Timeline Timeline

The target element from which to get the value.

Returns
string string string

The Storyboard.TargetProperty value of the target element.

Attributes

Pause() Pause() Pause()

Pauses the animation clock associated with the storyboard.

public void Pause()public void Pause()Public Function Pause() As void
Attributes

Resume() Resume() Resume()

Resumes the animation clock, or run-time state, associated with the storyboard.

public void Resume()public void Resume()Public Function Resume() As void
Attributes

Seek(TimeSpan) Seek(TimeSpan) Seek(TimeSpan)

Moves the storyboard to the specified animation position. The storyboard performs the requested seek when the next clock tick occurs.

public void Seek(TimeSpan offset)public void Seek(TimeSpan offset)Public Function Seek(offset As TimeSpan) As void
Parameters
offset
TimeSpan TimeSpan TimeSpan

A positive or negative time value that describes the amount by which the timeline should move forward or backward from the beginning of the animation.

Attributes

SeekAlignedToLastTick(TimeSpan) SeekAlignedToLastTick(TimeSpan) SeekAlignedToLastTick(TimeSpan)

Moves the storyboard to the specified animation position immediately (synchronously).

public void SeekAlignedToLastTick(TimeSpan offset)public void SeekAlignedToLastTick(TimeSpan offset)Public Function SeekAlignedToLastTick(offset As TimeSpan) As void
Parameters
offset
TimeSpan TimeSpan TimeSpan

A positive or negative time value that describes the amount by which the timeline should move forward or backward from the beginning of the animation.

Attributes

SetTarget(Timeline, DependencyObject) SetTarget(Timeline, DependencyObject) SetTarget(Timeline, DependencyObject)

Causes the specified Timeline to target the specified object.

public static void SetTarget(Timeline timeline, DependencyObject target)public static void SetTarget(Timeline timeline, DependencyObject target)Public Static Function SetTarget(timeline As Timeline, target As DependencyObject) As void
Parameters
timeline
Timeline Timeline Timeline

The timeline that targets the specified dependency object.

target
DependencyObject DependencyObject DependencyObject

The actual instance of the object to target.

Attributes

SetTargetName(Timeline, String) SetTargetName(Timeline, String) SetTargetName(Timeline, String)

Sets the value of the Storyboard.TargetName XAML attached property for a target element.

public static void SetTargetName(Timeline element, String name)public static void SetTargetName(Timeline element, String name)Public Static Function SetTargetName(element As Timeline, name As String) As void
Parameters
element
Timeline Timeline Timeline

The target element to set the value for.

name
System.String System.String System.String

The Storyboard.TargetName value of the target element to set. This should correspond to an existing Name or x:Name value on the UI element that the animation targets.

Attributes

SetTargetProperty(Timeline, String) SetTargetProperty(Timeline, String) SetTargetProperty(Timeline, String)

Sets the value of the Storyboard.TargetProperty XAML attached property for a target element.

public static void SetTargetProperty(Timeline element, String path)public static void SetTargetProperty(Timeline element, String path)Public Static Function SetTargetProperty(element As Timeline, path As String) As void
Parameters
element
Timeline Timeline Timeline

The target element for which to set the value.

path
System.String System.String System.String

The Storyboard.TargetProperty value of the target element to set. This specifies a qualification path that targets the dependency property where the animation applies. See Remarks.

Attributes

Remarks

Using SetTargetProperty in code as opposed to targeting an animation in initial XAML definitions is rare. It's tricky to get all the targeting and timing correct in an animation, particularly if you're trying to incorporate values that are only available at run time. We recommend that you create your animations in XAML, which means you'll use the Storyboard.TargetProperty attached property, not the SetTargetProperty runtime method (which supports the XAML behind the scenes). Building up animations in code is an advanced scenario. Here's a very basic code example of what's involved:

            Rectangle rect = new Rectangle();
            rect.RenderTransform = new ScaleTransform();
            //TODO - connect this Rectangle to the visual tree
            Storyboard storyboard = new Storyboard();
            DoubleAnimation scalex = new DoubleAnimation()
            {
                From = 0,
                To = 8,
                AutoReverse = true,
                Duration = TimeSpan.FromSeconds(2)
            };
            Storyboard.SetTargetProperty(scalex, "(Rectangle.RenderTransform).(ScaleTransform.ScaleX)");
            Storyboard.SetTarget(scalex, rect);
            //TODO - Begin the animation

Property paths for animation property targeting

The Storyboard.TargetProperty attached property is typically set on the individual Timeline -derived animations that make up the Storyboard.Children collection of an animation definition in XAML.

The Storyboard.TargetName property can process a string syntax that enables targeting a subproperty of a property value. The syntax uses a "dot-down" metaphor for targeting a chain of object-property relationships until a particular subproperty is identified. This enables animations to apply to the value types where there is a supporting animation structure (Double, Color, Point, and Object for DiscreteObjectKeyFrameAnimation ). For example, you might want to animate the Background value of a Control, which takes an object type of Brush. There is no "BrushAnimation" animation type, so you cannot directly target an animation for Background . But what you can do instead is reference a SolidColorBrush subproperty that is named Color, which takes type Color and can thus be targeted by a ColorAnimation. The string syntax for this is:

(Control.Background).(SolidColorBrush.Color)

The parentheses around "(Control.Background)" inform the processing that the intermediate "dot" should not "dot down" and is instead part of the qualification name that finds the ownertype-qualified Background property for targeting. The following "dot" is treated as a "dot-down" instruction, which requests a subproperty of the Brush type. The final "(SolidColorBrush.Color)" fragment again includes the parentheses so that the interior "dot" is again used as ownertype.member qualification, not a "dot-down".

Note that for subproperty values, there can be some value inference. For example, the following string works by inference even though "Color" is actually a value of the particular Brush subclass SolidColorBrush:

(Control.Background).Color

There is a lot more to property path specification than this. This remark is just intended to get you started with the basic targeting scenarios. For more info, see Property-path syntax and Storyboarded animations.

Migration notes

When you specify a path value for the path parameter, you specify the string, and if you retrieve the value again using GetTargetProperty, you also get the value as a string. This is in contrast to some other implementations of animation property targeting concepts such as Microsoft Silverlight and Windows Presentation Foundation (WPF). These XAML technologies use a representative object type (a discrete PropertyPath object) to store the property path information for animation targeting, and the syntax for the SetTargetProperty methods use the PropertyPath type. The Windows Runtime also has a PropertyPath class; however that class is only used for data binding, which is another scenario for property path definitions. The Windows Runtime doesn't support modifying a property path in a Storyboard after it's defined using the string, so its implementation of Storyboard.TargetProperty takes a string. This distinction doesn't even matter for XAML syntax and attached property support. It only matters for creating dynamic storyboards in code, or programmatically modifying storyboard values after they are initially defined in XAML. If you have any need to retarget an animation to a different property at run time, create an entirely new Storyboard with the new property path string, run animations with the new Storyboard, and stop using the previous one.

See Also

SkipToFill() SkipToFill() SkipToFill()

Advances the current time of the storyboard's clock to the end of its active period.

public void SkipToFill()public void SkipToFill()Public Function SkipToFill() As void
Attributes

Remarks

If you call this method on an animation that doesn't have a FillBehavior value of HoldEnd, this has the same effect as calling Stop.

This method is mainly relevant for methods that are playing or paused, and which don't have infinite repeat. Calling SkipToFill on an infinite animation or an animation that's already stopped has no effect.

See Also

Stop() Stop() Stop()

Stops the storyboard.

public void Stop()public void Stop()Public Function Stop() As void
Attributes

See Also