Reputation: 1246
It seems like it should be so simple. I've read dozens of links and I can't get anything to animate the position. I believe the closest code I can write so far is this:
Storyboard storyboard = new Storyboard();
TranslateTransform trans = new TranslateTransform() { X = 1.0, Y = 1.0 };
myCheckbox.RenderTransformOrigin = new Point(0.5, 0.5);
myCheckbox.RenderTransform = trans;
DoubleAnimation moveAnim = new DoubleAnimation();
moveAnim.Duration = TimeSpan.FromMilliseconds(1200);
moveAnim.From = -1;
moveAnim.To = 1;
Storyboard.SetTarget(moveAnim, myCheckbox);
Storyboard.SetTargetProperty(moveAnim, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
storyboard.Completed += new System.EventHandler(storyboard_Completed);
storyboard.Children.Add(moveAnim);
storyboard.Begin();
No errors are thrown. The completion callback does get called. If I animate opacity in a similar fashion it works fine.
How can I simply animate a UIElement's position with code??
Upvotes: 3
Views: 2043
Reputation: 1246
The comment from xyzzer was correct. The cause of the confusion was because the coordinates for RenderTransformOrigin
use (0,1) relative to the element. The actual transforms (e.g. TranslateTransform
) use pixels as units.
Upvotes: 3