Animation in XAML (Part 2)

Introduction

This is the second of my series of posts on animation, using WPF and XAML. In this post, I’m looking at how we can control an animation in WPF using code behind, and also, how two animations can be triggered simultaneously.

Button Animation in WPF with code-behind

The problem with a statement like this, is that you can pretty much replicate anything in XAML in code, so you can do everything in code, some, a little or nothing. So far, I’ve come across three approaches that make sense to me. In this article I’ll cover triggering animations from code behind; following articles will cover creating animations for elements that have the transform set in XAML, and finally, no XAML at all!

Triggering an animation from code

There’s little point in re-explaining the XAML; it’s essentially the same XAML that I’ve shown in the first post. It looks like this:

    <Window.Resources>
        <Storyboard x :Key="sbExpand">
            <DoubleAnimation
                Storyboard.TargetName="codebehindbutton"
                Storyboard.TargetProperty="(Button.RenderTransform).(ScaleTransform.ScaleX)"
                From="0" To ="1.2" Duration="0:0:0.3"/>
            <DoubleAnimation
                Storyboard.TargetName="codebehindbutton"
                Storyboard.TargetProperty="(Button.RenderTransform).(ScaleTransform.ScaleY)"
                From="0" To ="1.2" Duration="0:0:0.3"/>
       
            <DoubleAnimation
                Storyboard.TargetName="codebehindbutton"
                Storyboard.TargetProperty="(Button.RenderTransform).(ScaleTransform.ScaleX)"
                From="1.2" To ="1" Duration="0:0:0.3"
                BeginTime="0:0:0.3"/>
            <DoubleAnimation
                Storyboard.TargetName="codebehindbutton"
                Storyboard.TargetProperty="(Button.RenderTransform).(ScaleTransform.ScaleY)"
                From="1.2" To ="1" Duration="0:0:0.3"
                BeginTime="0:0:0.3"/>
        </Storyboard>
  

So, there’s a few things to note here:
– The storyboard is not part of the button definition; it’s actually part of the Window.Resources.
– We don’t use a named transition, but instead, specify the button name; however, we still have to tell the button that it has a transition:

    <Button Name ="codebehindbutton" Width="200" Height="150" Click="Button_Click" BorderThickness="2" Content="Don't Click Me">
          <Button.RenderTransform>
              <ScaleTransform 
                         ScaleX="1" ScaleY ="1" CenterX="100" CenterY="175"  />
          </Button.RenderTransform>
    </Button>

Generally speaking, there’s nothing new. The same transformation, the same effect; the only difference is that it’s defined as a resource for the Window rather than for the Button.

So, now we get to the crux; now that it doesn’t fire on a button event, how does it get fired? As usual with these things, it’s surprisingly easy when you look at the code:

    Storyboard sb = (Storyboard )FindResource("sbExpand");
    sb.Begin( this);

You also have access to the storyboard itself; for example, try this:

    Storyboard sb = (Storyboard )FindResource( "sbExpand");
    sb.AutoReverse = true;
    sb.Begin( this);

Multiple Animations

Cool eh?

Additionally, if you had two animations to start at the same time; this would be the way. For example, add the following label to your page:

         <Label x :Name="lblHello" Content="Spin Me!" Margin ="50,152,417,250" Width="50" Height="30"
               RenderTransformOrigin="0.5,0.5">
            <Label.RenderTransform>
                <RotateTransform/>
            </Label.RenderTransform>
        </Label>

And the following storyboard definition:

         <Storyboard x :Key="sbdLabelRotation">
            <DoubleAnimation
              Storyboard.TargetName="lblHello"
              Storyboard.TargetProperty="(Label.RenderTransform).(RotateTransform.Angle)"
              From="0"
              To="360"
              Duration="0:0:3"
              RepeatBehavior="4x" />
        </Storyboard>

(This was taken from the Laurent Bugnion article on the same subject http://www.galasoft.ch/mydotnet/articles/article-2006102701.aspx).

Now, try running both animations together:

             Storyboard sb = (Storyboard )FindResource("sbExpand");           
            sb.Begin( this);

             Storyboard sbdLabelRotation = (Storyboard)FindResource("sbdLabelRotation" );
            sbdLabelRotation.Begin( this);

Conclusion

So, both animations run at the same time. Admittedly, I’m not strictly sure what you could do with this particular animation; it’s certainly not my intention that WPF apps start taking a mid ’90s web-site feel!

In this article, we looked at controlling pre-defined storyboard definitions in code. In a future article, I intend to cover the creation of an animation from within code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.