Click here to Skip to main content
Email Password   helpLost your password?
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
{
    /// 
    /// Interaction logic for Window1.xaml
    /// 
    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
{
    /// 
    /// Interaction logic for Window1.xaml
    /// 
    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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSlightly deeper and more general
mike.james
4:01 25 Jul '09  
I found the discussion in WPF how and why helpful and it deals with Animation and graphics in general at the end.
GeneralSome issues with the tutorial
GHP9170
4:45 8 May '09  
There is a small typo where you in the following line,

da.RepeatBehavior = RepeatBehavior.Forever


should be

da.RepeatBehavior = RepeatBehavior.Forever;


Also this article indicates that setting the behavior of 'da' should occur after the BeginAnmiation command. I have had issues with this working properly once the BeginAnimation method is invoked you can't set properties of 'da' and have them applied to the current animation.

The solution to this is setting all properties of 'da' before you invoke BeginAnimation.

Please respond if I am incorrect, Otherwise so far its a great article!
GeneralBeginner's WPF Animation Tutorial
zaurs
12:10 4 Mar '09  
Perfect, nice and simple demonstration for the beginner. Really good understanding of what's so impressive about WPF for the newcomer. Thanks so much!
GeneralRe: Beginner's WPF Animation Tutorial
NinethSense
20:15 4 Mar '09  
Thank you Smile

PraVeeN
blog.ninethsense.com/

GeneralLittle explanation appreciated
debashish 1979
10:15 30 Dec '08  
NinethSense ... Thanks for sharing this beginner's article with community!

To improve further, explain your code little bit as why did you use DoubleAnimation etc.

Its fine to see some code doing something but it is great to understand why and how. Smile
GeneralRe: Little explanation appreciated
NinethSense
19:34 30 Dec '08  
I will try explaining... soon Smile

PraVeeN
blog.ninethsense.com/

GeneralProblem with the Rectangle [modified]
Ismail_K
22:07 5 Jun '08  
Hi NinethSense,

I would like to thank you for these nice tutorials. I have tried to download the code with the Wheel and it worked.

Now i was trying to do the rectangle example. It compiles correctly but No action is associated with the clik on the button.

I will join you my code so that you have a look at my code.



C# code:
using System;
using System.Collections.Generic;
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.Shapes;
using System.Windows.Media.Animation;


namespace example2
{
///
/// Interaction logic for Window1.xaml
///


public partial class Window1 : System.Windows.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);
}
}
}



XAML code:

"example2.Window1">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="example2" Height="300" Width="300"
>

<button margin="140,111,77,133" name="button1">Button</button>



rectangle height="12" fill="Blue" margin="73,0,70,79.5" verticalalignment="Bottom" name="rectangle1"





I cannot right the Xaml code correctly , a bug in the post i think.
But i have put < /> for the rectangle and i've closed grid and Window at the end


I am using Visual Studio 2005, .Net Framework 3.0
Your Help is apreciated,
Ismail
 

modified on Friday, June 6, 2008 3:23 AM

GeneralRe: Problem with the Rectangle
NinethSense
19:35 30 Dec '08  
Sorry, I missed this post. Problem solved?

PraVeeN
blog.ninethsense.com/

GeneralNice example :) but one thing:
FreeRider1
4:54 6 May '08  
Window1.xaml

Source contains absolute path:
Source="file:///C:/Documents and Settings/Praveen/Desktop/WpfApplication1/bin/Release/wheel.png

I think it's better to change to relative Smile I was surprised first to see the empty window.
GeneralRe: Nice example :) but one thing:
NinethSense
9:57 6 May '08  
Yes. Smile
Thanks

PraVeeN
blog.ninethsense.com/

Generalabnormal
Eric Gunnerson
3:07 28 Jan '08  
You yourself is telling dat you are abnormal .So i am excusing you for writing this article...

Shucks
GeneralRe: abnormal
NinethSense
3: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
wasimsharp
0:01 14 Oct '08  
ya then why u should not write any article better then that. Mad

wasim khan

GeneralRe: abnormal
LekshmiM
20:07 11 Mar '09  
very nice article Smile Rose


Last Updated 28 Jan 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010