How do I animate UserControl objects using Storyboard and DoubleAnimation?
Posted
by Neo
on Stack Overflow
See other posts from Stack Overflow
or by Neo
Published on 2009-11-24T12:17:36Z
Indexed on
2010/05/24
17:31 UTC
Read the original article
Hit count: 378
In my WPF application, I have a Canvas object that contains some UserControl objects.
I wish to animate the UserControl objects within the Canvas using DoubleAnimation
so that they go from the right of the Canvas to the left of the Canvas. This is how I have done it so far (by passing the UserControl objects into the function):
private void Animate(FrameworkElement e)
{
DoubleAnimation ani = new DoubleAnimation()
{
From = _container.ActualWidth,
To = 0.0,
Duration = new Duration(new TimeSpan(0, 0, 10),
TargetElement = e
};
TranslateTransform trans = new TranslateTransform();
e.RenderTransform = trans;
trans.BeginAnimation(TranslateTransform.XProperty, ani, HandoffBehavior.Compose);
}
However, this doesn't allow me to pause the animation, so I have considered using a Storyboard instead to do this, but I'm not sure how to implement this. This has been my attempt so far:
private void Animate(FrameworkElement e)
{
DoubleAnimation ani = new DoubleAnimation()
{
From = _container.ActualWidth,
To = 0.0,
Duration = new Duration(new TimeSpan(0, 0, 10),
TargetElement = e
};
Storyboard stb = new Storyboard();
Storyboard.SetTarget(ani, e);
Storyboard.SetTargetProperty(ani, "Left");
stb.Children.Add(ani);
stb.Begin();
}
Of course, this fails as UserControl doesn't have a Left
property. How can I achieve what I'm after?
Thanks.
© Stack Overflow or respective owner