user1272388 user1272388 - 25 days ago 6
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(() =>
{
while(true){
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;

sb.Children.Add(myDoubleAnimation1);

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.
sb.Begin();}
});

LPL LPL
Answer

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

Edit:

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

myThicknessAnimation1.RepeatBehavior = RepeatBehavior.Forever;