Welcome to Windows Presentation Foundation (WPF)
Top Tasks :

WPF Team Bloggers

Wednesday, April 30, 2008 - Posts

  • BeginAnimation() method for Silverlight

    Yeah, it's been awhile since I've blogged anything, so I thought a nice way to get back in the game was to post a BeginAnimation method. Sometimes you just want to start a simple animation in code, and the Silverlight way of doing it through storyboards just seems like more code that you feel like typing, which is where the WPF-inspired BeginAnimation comes in. Turns out you can do a pretty good BeginAnimation API yourself, using C# extension methods -- just drop the following code into your project: public static class Helper { public static void BeginAnimation(this FrameworkElement e, string prop, Timeline t) { var sb = new Storyboard(); e.Resources.Add(sb); sb.Children.Add(t); Storyboard.SetTarget(sb, e); Storyboard.SetTargetProperty(sb, prop); sb.Begin(); } } Which can be used: var tb = (TextBox)sender; var an = new DoubleAnimation(); an.From = 100; an.To = 200; tb.BeginAnimation("Height", an); (Okay, it doesn't handle all the corner cases quite like WPF, in particular calling BeginAnimation Read More...

Copyright © 2006 Microsoft Corporation. All Rights Reserved. | Terms of Use | Privacy Statement | Contact Us