How to Animate Transforms in code behind

2019-03-06 14:51发布

问题:

I have a custom Button. For some reason, I would like to apply transforms in code behind. And The transforms work fine. But when I try to add animations on the transforms, the animations don't take effect. My code is as below.

    public partial class MyButton : Button
    {
        private TranslateTransform translate = new TranslateTransform() { X = 200, Y = 100 };

        public MyButton()
        {
            InitializeComponent();
            this.RenderTransform = new TransformGroup()
            {
                Children = 
                {
                    this.translate
                }
            };
        }

        protected override void OnClick()
        {
            base.OnClick();
            // Edit: I need to use Storyboard to synchronized animation1 and animation2.
            var sb = new Storyboard();
            var animation1 = new DoubleAnimation(200, -10, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
            Storyboard.SetTarget(animation1, this.translate);
            Storyboard.SetTargetProperty(animation1, new PropertyPath("X"));
            sb.Children.Add(animation1);

            var animation2 = new DoubleAnimation(100, 0, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
            Storyboard.SetTarget(animation2, this.translate);
            Storyboard.SetTargetProperty(animation2, new PropertyPath("Y"));
            sb.Children.Add(animation2);                
            sb.Begin(this);
        }
    }

Could anyone explain why and how to add animations for this scenario?

回答1:

OP says he needs a storyboard after all. The trouble with that seems to be that sometimes SetTarget() doesn't work. I don't know why not, I didn't look into that. I just tested the workaround, and found it good.

Note that you have to do the NameScope/RegisterName stuff in the constructor.

public MyButton()
{
    InitializeComponent();

    this.RenderTransform = new TransformGroup()
    {
        Children =
        {
            this.translate
        }
    };

    NameScope.SetNameScope(this, new NameScope());
    RegisterName("translate", this.translate);
}

protected override void OnClick()
{
    base.OnClick();

    var sb = new Storyboard();
    var animation1 = new DoubleAnimation(200, -10, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
    Storyboard.SetTargetName(animation1, "translate");
    Storyboard.SetTargetProperty(animation1, new PropertyPath(TranslateTransform.XProperty));
    sb.Children.Add(animation1);

    var animation2 = new DoubleAnimation(100, 0, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
    Storyboard.SetTargetName(animation2, "translate");
    Storyboard.SetTargetProperty(animation2, new PropertyPath(TranslateTransform.YProperty));
    sb.Children.Add(animation2);

    sb.Begin(this);
}

Simple Solution

You don't need to create two different animations with the same parameters. Turns out you don't need a storyboard either; that's for starting an animation for an event or something. I don't know why you didn't google this. Google has it in autocomplete for "wpf storyboard animation...". It's here, anyway.

protected override void OnClick()
{
    base.OnClick();

    var animation1 = new DoubleAnimation(100, 0, 
                         new Duration(new TimeSpan(0, 0, 0, 1, 0)));

    translate.BeginAnimation(TranslateTransform.XProperty, animation1);
    translate.BeginAnimation(TranslateTransform.YProperty, animation1);
}