Home > Mobile >  How can I clear animation of Canvas.Left?
How can I clear animation of Canvas.Left?

Time:01-29

I created an animation of Dependency Property Canvas.Left with code:

DoubleAnimation myDoubleAnimation = new DoubleAnimation { From =1, To = 100, Duration = new Duration(TimeSpan.FromMilliseconds(500)) };
Storyboard.SetTarget(myDoubleAnimation, Image);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(Canvas.Left)"));
Storyboard myMovementStoryboard = new Storyboard();
myMovementStoryboard.Children.Add(myDoubleAnimation);
myMovementStoryboard.Begin();

After the animation, I need to clear it with

UIElement.BeginAnimation(DependencyProperty, AnimationTimeline)

by a null AnimationTimeline.

Otherwise, the Canvas.Left will never change after I modify it by code.

However, the UIElement.BeginAnimation only accepts DependencyProperty but not a PropertyPath.

How can I solve this? Thank you.

CodePudding user response:

You would call

Image.BeginAnimation(Canvas.LeftProperty, null);

The Storyboard is redundant. You could as well call

Image.BeginAnimation(Canvas.LeftProperty, myDoubleAnimation);

You may also avoid that the animation holds the value by setting

myDoubleAnimation = new DoubleAnimation
{
    From = 1,
    To = 100,
    Duration = TimeSpan.FromMilliseconds(500),
    FillBehavior = FillBehavior.Stop
};
  • Related