Applying animated ScaleTransform in code problem

本秂侑毒 提交于 2019-12-01 03:42:24

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!

Here is an example of how to animate in two different directions on a ScaleTransform, when you have a transform group. The path string shows which part is being animated. Also, because Canvas is freezable, you have to RegisterName. (I don't know what this means, but it is required)

        var storyBoard = new Storyboard();
        var group = new TransformGroup();
        var scale = new ScaleTransform(Zoom, Zoom);
        group.Children.Add(scale);
        group.Children.Add(new TranslateTransform(_translateX,_translateY));
        MainCanvas.RenderTransform = group;

        RegisterName("MainCanvas",MainCanvas);

        var growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(1000);
        growAnimation.From = _oldZoom;
        growAnimation.To = Zoom;
        storyBoard.Children.Add(growAnimation);

        var growAnimation2 = new DoubleAnimation();
        growAnimation2.Duration = TimeSpan.FromMilliseconds(1000);
        growAnimation2.From = _oldZoom;
        growAnimation2.To = Zoom;

        storyBoard.Children.Add(growAnimation2);

        string thePath = "(0).(1)[0].(2)"; // Not used - just to show the syntax


        Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.Children[0].ScaleX"));
        Storyboard.SetTargetProperty(growAnimation2, new PropertyPath("RenderTransform.Children[0].ScaleY"));
        Storyboard.SetTargetName(growAnimation, "MainCanvas");
        Storyboard.SetTargetName(growAnimation2,"MainCanvas");
        storyBoard.Begin(this);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!