Programatically WPF Fade In (via extension methods)

Posted by Dinis Cruz on Stack Overflow See other posts from Stack Overflow or by Dinis Cruz
Published on 2010-06-15T22:57:37Z Indexed on 2010/06/15 23:02 UTC
Read the original article Hit count: 243

Filed under:
|
|

I'm trying to write a simple (stand alone) C# extension method to do a Fade-In of a generic WPF UIElement, but the solutions (and code samples) that I found all contain a large number of moving parts (like setting up a story, etc...)

For reference here is an example of the type of API method I would like to create. This code will rotate an UIElement according to the values provided (fromValue, toValue, duration and loop)

public static T rotate<T>(this T uiElement, double fromValue, double toValue, int durationInSeconds, bool loopAnimation)
    where T : UIElement
{
    return (T)uiElement.wpfInvoke(
            ()=>{
                    DoubleAnimation doubleAnimation = new DoubleAnimation(fromValue, toValue, new Duration(TimeSpan.FromSeconds(durationInSeconds)));
                    RotateTransform rotateTransform = new RotateTransform(); 
                    uiElement.RenderTransform = rotateTransform;
                    uiElement.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);  
                    if (loopAnimation)
                        doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
                    rotateTransform.BeginAnimation(RotateTransform.AngleProperty, doubleAnimation);
                    return uiElement;
                });
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about wpf