Click here to Skip to main content
Licence CPOL
First Posted 27 Jan 2008
Views 196,449
Downloads 7,530
Bookmarked 104 times

Beginner's WPF Animation Tutorial

By NinethSense | 28 Jan 2008
A simple animation tutorial on WPF
6 votes, 12.8%
1

2
6 votes, 12.8%
3
9 votes, 19.1%
4
26 votes, 55.3%
5
4.44/5 - 47 votes
6 removed
μ 3.67, σa 2.40 [?]
WpfApplication2

Introduction

My expected audience for this article is extreme beginners of WPF. But you should be knowledgeable in any of the .NET CLR languages. I used my favorite C# for description in this article. Also, do not forget, WPF is for .NET Framework 3.x+ and I used Visual Studio Express 2008.

Animate the Button

This tutorial is for creating a simple button animation with System.Windows.Media.Animation namespace. As usual, I use C# for demonstrating this sample since it is my favorite after C language. Also note that I write these articles for programmers and I will be writing code in C# even though we can do all these with XAML itself.

Step 1: Place a button on the form. We will call it Button1.

Step 2: Now add these lines to the button click event. (Simply double click on the button if you are in Visual Studio). Remember to set the language of your code snippet using the language dropdown.

DoubleAnimation da = new DoubleAnimation();
da.From = 30;
da.To = 100;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
Button1.BeginAnimation(Button.HeightProperty, da);

Step 3: Press F5. i.e. Execute.

You will see a button increase its size automatically when you click.

Tips

  1. Add the following line before BeginAnimation. This will restore the button back after the animation. Of course it is also animated.

    da.AutoReverse = true;
  2. Add the following line before BeginAnimation. You can see that the animation never stops.

    da.RepeatBehavior = RepeatBehavior.Forever

Rotate Rectangle

WpfApplication2

Step 1: Place a button and shape rectangle on the form. (Button is not needed. I use it just for raising an event.)

Step 2: Add the following code in the button click event:
Note: Do not forget to import System.Windows.Media.Animation namespace.

DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = 360;
da.Duration = new Duration(TimeSpan.FromSeconds(3));
da.RepeatBehavior = RepeatBehavior.Forever;
RotateTransform rt = new RotateTransform();
rectangle1.RenderTransform = rt;
rt.BeginAnimation(RotateTransform.AngleProperty, da);

Step 3: Execute and enjoy. You can see a rectangle rotating 360 degrees continuously. As I mentioned in the other article, you can add autoreverse etc.

Here is the complete C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
 
namespace WpfApplication1
{
    /// <summary />
    /// Interaction logic for Window1.xaml
    /// </summary />
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation da = new DoubleAnimation();
            da.From = 0;
            da.To = 360;
            da.Duration = new Duration(TimeSpan.FromSeconds(3));
            da.RepeatBehavior = RepeatBehavior.Forever;
            RotateTransform rt = new RotateTransform();
            rectangle1.RenderTransform = rt;
            rt.BeginAnimation(RotateTransform.AngleProperty, da);
        }
    }
}

Artificially Rotate a Wheel

In this article, you can see a wheel picture at the top. We will use image1.RenderTransformOrigin to keep the centerpoint of the image. Check the sample source code attached.

Here is the code for 'running wheel':

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace WpfApplication1
{
    /// <summary />
    /// Interaction logic for Window1.xaml
    /// </summary />
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation da = new DoubleAnimation
                (360, 0, new Duration(TimeSpan.FromSeconds(3)));
            RotateTransform rt = new RotateTransform();
            image1.RenderTransform = rt;
            image1.RenderTransformOrigin = new Point(0.5, 0.5);
            da.RepeatBehavior = RepeatBehavior.Forever;
            rt.BeginAnimation(RotateTransform.AngleProperty, da);
        }
    }
}

The animation procedure specified in this article is simply 'nothing' when compared to the scope hidden in WPF. Hope the fear that beginners have will be wiped out with this sample article. WPF is as simple as ABC.

History

  • 28th January, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

NinethSense

Architect
Independent IT Consultant
India India

Member

Follow on Twitter Follow on Twitter
Praveen.V.Nair - aka NinethSense - Microsoft MVP - is a person with an abnormal passion for technology. He has been playing with electronics from the age of 10 and with computers from the age of 14. He usually blogs at http://blog.ninethsense.com/.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralC# Pinmemberpengdan17:35 28 Dec '11  
GeneralMy vote of 5 Pinmemberashwin_nair20113:25 4 Nov '11  
Questionhow to connect winform and wpf in c# Pinmembershamilmca0:14 13 Feb '11  
AnswerRe: how to connect winform and wpf in c# PinmemberRavi Bhavnani10:03 8 Apr '11  
GeneralMy vote of 5 Pinmember_dado_22:30 30 Jan '11  
GeneralMy vote of 5 Pinmembercatfish10123:03 4 Jan '11  
GeneralAnimation [modified] Pinmemberadil_iise4:20 1 Jan '11  
GeneralThanx PinmemberAndre K3:36 22 Dec '10  
GeneralWPF (Windows Presentation Foundation) – Introduction and Sample Code in C# .NET Pinmemberdummyahuja6:43 25 Oct '10  
GeneralMy vote of 5 PinmemberSandun Rajapakshe9:12 12 Jun '10  
GeneralIsItemHost property Pinmembermadala1612:07 10 May '10  
GeneralSlightly deeper and more general Pinmembermike.james4:01 25 Jul '09  
GeneralSome issues with the tutorial PinmemberGHP91704:45 8 May '09  
GeneralBeginner's WPF Animation Tutorial Pinmemberzaurs12:10 4 Mar '09  
GeneralRe: Beginner's WPF Animation Tutorial PinmemberNinethSense20:15 4 Mar '09  
GeneralLittle explanation appreciated Pinmemberdebashish 197910:15 30 Dec '08  
GeneralRe: Little explanation appreciated PinmemberNinethSense19:34 30 Dec '08  
GeneralProblem with the Rectangle [modified] PinmemberIsmail_K22:07 5 Jun '08  
GeneralRe: Problem with the Rectangle PinmemberNinethSense19:35 30 Dec '08  
GeneralNice example :) but one thing: PinmemberFreeRider14:54 6 May '08  
GeneralRe: Nice example :) but one thing: PinmemberNinethSense9:57 6 May '08  
Generalabnormal PinmemberEric Gunnerson3:07 28 Jan '08  
GeneralRe: abnormal PinmemberNinethSense3:21 28 Jan '08  
But dude, I am curious to know the reason for your comment. If what I wrote is wrong, I have to remove this article. Thanks for the feedback.
 
PraVeeN
blog.ninethsense.com/

GeneralRe: abnormal Pinmemberwasimsharp0:01 14 Oct '08  
GeneralRe: abnormal PinmemberLekshmiM20:07 11 Mar '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 28 Jan 2008
Article Copyright 2008 by NinethSense
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid