0

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?

Newton Zou
  • 558
  • 1
  • 5
  • 20

1 Answers1

2

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);
}
Community
  • 1
  • 1
  • Thanks for this information. But actually, I still need a Storyboard. The code in OnClick is just a simplified code. I don't want the animation to start right away, because I have a lot of animations to begin at the same time. I need first to create the Storyboard, then to begin it on some conditions. – Newton Zou Mar 08 '17 at 04:15
  • @NewtonZou Updated with solution – 15ee8f99-57ff-4f92-890c-b56153 Mar 08 '17 at 04:34
  • Perfect! excellent! wonderful! For NameScope/RegisterName stuff, I guess that is being called behind the scenes when we did the animation in XAML. – Newton Zou Mar 08 '17 at 05:29