Applying animated ScaleTransform in code problem

Marcel picture Marcel · Jan 25, 2010 · Viewed 23.7k times · Source

I am trying to find out why the code below does not seem to work. It does not give an error - it simply doesn't scale. It actually does seem to work if I change it as to my second code sample. Anyone got any idea?

Thanks

public static void StartMouseEnterAnimation(Button button)
    {
        Storyboard storyboard = new Storyboard();

        ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1);
        button.RenderTransformOrigin = new Point(0.5, 0.5);
        button.RenderTransform = scale;

        DoubleAnimation growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(300);
        growAnimation.From = 1;
        growAnimation.To = 1.8;
        storyboard.Children.Add(growAnimation);

        Storyboard.SetTargetProperty(growAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
        Storyboard.SetTarget(growAnimation, scale);

        storyboard.Begin();
    }

--- The following DOES work but I had to create a TransformGroup and reference this through a more complicated PropertyChain...

public static void StartMouseEnterAnimation(Button button)
    {    
        Storyboard storyboard = new Storyboard();            
        ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1);
        button.RenderTransformOrigin = new Point(0.5, 0.5);
        TransformGroup myTransGroup = new TransformGroup();
        myTransGroup.Children.Add(scale);
        button.RenderTransform = myTransGroup;

        DoubleAnimation growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(100);
        //growAnimation.From = 1;
        growAnimation.To = 1.1;
        storyboard.Children.Add(growAnimation);

        DependencyProperty[] propertyChain = new DependencyProperty[]
        {
            Button.RenderTransformProperty, 
            TransformGroup.ChildrenProperty,
            ScaleTransform.ScaleXProperty
        };
        string thePath = "(0).(1)[0].(2)";
        PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain);
        Storyboard.SetTargetProperty(growAnimation, myPropertyPath);
        Storyboard.SetTarget(growAnimation, button);

        storyboard.Begin();
   }

Answer

Mark Carpenter picture Mark Carpenter · Jul 13, 2010

I was able to get it to work by tweaking your first code sample like so:

public static void StartMouseEnterAnimation(Button button) {
    Storyboard storyboard = new Storyboard();

    ScaleTransform scale = new ScaleTransform(1.0, 1.0);
    button.RenderTransformOrigin = new Point(0.5, 0.5);
    button.RenderTransform = scale;

    DoubleAnimation growAnimation = new DoubleAnimation();
    growAnimation.Duration = TimeSpan.FromMilliseconds(300);
    growAnimation.From = 1;
    growAnimation.To = 1.8;
    storyboard.Children.Add(growAnimation);

    Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.ScaleX"));
    Storyboard.SetTarget(growAnimation, button);

    storyboard.Begin();
}

Instead of new PropertyPath(ScaleTransform.ScaleXProperty)), I used new PropertyPath("RenderTransform.ScaleX")), and I set the target of the storyboard to the button (not the scaleTransform itself).

Hope that helps!