How can I animate the Margin of a Stackpanel with a storyboard?
10,149
Solution 1
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;
Solution 2
Here is the working example, if anybody needs:
//Animate margin for Label, named "label" from right to left. from 300 to 0.
var sb = new Storyboard();
var ta = new ThicknessAnimation();
ta.BeginTime = new TimeSpan(0);
ta.SetValue(Storyboard.TargetNameProperty, "label");
Storyboard.SetTargetProperty(ta, new PropertyPath(MarginProperty));
ta.From = new Thickness(300, 30, 0, 0);
ta.To = new Thickness(0, 30, 0, 0);
ta.Duration = new Duration(TimeSpan.FromSeconds(3));
sb.Children.Add(ta);
sb.Begin(this);
Author by
user1272388
Updated on June 05, 2022Comments
-
user1272388 almost 2 years
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();} });