Thursday, July 9, 2009

Minimum Code for Silverlight Rotation Animation

This is the minimum code for animate rotating a Silverlight element (a variable named Element):

Element.RenderTransformOrigin = new Point(0.5, 0.5);
Element.RenderTransform = new RotateTransform();
DoubleAnimation da = new DoubleAnimation
{
From = 0,
To = 360,
Duration = TimeSpan.FromSeconds(0.5)
};
Storyboard.SetTarget(da, Element.RenderTransform);
Storyboard.SetTargetProperty(da, new PropertyPath(RotateTransform.AngleProperty));
Storyboard sb = new Storyboard();
sb.Children.Add(da);
sb.Begin();

4 comments:

  1. I needed to move an object, but using your code it was super easy! Been to about 20 sites before I stumbled on this one -- well done sir!

    ReplyDelete
  2. I totally agree. I had to search way to hard to find this simple example. Thanks!

    ReplyDelete
  3. Sensational...had it working in seconds!

    ReplyDelete