user1272388 user1272388 - 10 months ago 53
C# Question

How can I animate the Margin of a Stackpanel with a storyboard?

I would like to use it, but it doesnt't work, I wanna create a tile animation, in code behind, or if you know a project for this gol, pls write me

Deployment.Current.Dispatcher.BeginInvoke(() =>
Duration duration = new Duration(TimeSpan.FromSeconds(0.15));

// Create two DoubleAnimations and set their properties.
DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();

myDoubleAnimation1.Duration = duration;
myDoubleAnimation1.From = -173
myDoubleAnimation1.To = 173;
Storyboard sb = new Storyboard();
sb.Duration = duration;


Storyboard.SetTarget(myDoubleAnimation1, image);

// Set the attached properties of Canvas.Left and Canvas.Top
// to be the target properties of the two respective DoubleAnimations.
Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath(StackPanel.MarginProperty));

// Begin the animation.

Answer Source

Use a ThicknessAnimation instead of a DoubleAnimation. It's almost the same.


If you want to make the Animation endless use Timeline.RepeatBehavior.

myThicknessAnimation1.RepeatBehavior = RepeatBehavior.Forever;