WPF: Animating TranslateTransform from code

前端 未结 4 873
梦如初夏
梦如初夏 2021-02-04 02:17

I have a WPF canvas on which I\'m dynamically creating objects from code. These objects are being transformed by setting the RenderTransform property, and an animation needs to

4条回答
  •  遇见更好的自我
    2021-02-04 02:41

    No need to register the transform or add the storyboard to the resource collection if the target is the FrameworkElement you want to animate. You can use Blend to generate the PropertyPath syntax.

    frameworkElement.RenderTransform = new TransformGroup
            {
                Children =
                {
                    new ScaleTransform(),
                    new SkewTransform(),
                    new RotateTransform(),
                    translate
                }
            };
    translate.X = 100.0;
    var animation = new DoubleAnimation(0.0, TimeSpan.FromSeconds(1));
    var sb = new Storyboard();            
    sb.Children.Add(animation);
    Storyboard.SetTarget(animation, frameworkElement);
    Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));
    sb.Begin();
    

提交回复
热议问题