Does anyone know of an easy way to animate a movement from an Image's current location to a new location (X,Y) using WPF animation with no XAML, 100% programmatically? And with no reference to "this" (with RegisterName etc).
I am trying to make an extension class for Image to do animation stuff on it. It is easy enough to change the width and height properties through animation, but after searching for location animation of an object it suddenly becomes more advanced.
As it is an extension class I will only have a reference to the actual Image object and the X and Y I want to move it to.
public static void MoveTo(this Image targetControl, double X, double Y, double Width, double Height){
//code here
...
}
Update:
Thanks. Almost working. It seems The GetTop and GetLeft returns 'NaN' not explicitly set. Found the workaround in this post: Canvas.GetTop() returning NaN
public static void MoveTo(this Image target, double newX, double newY) {
Vector offset = VisualTreeHelper.GetOffset(target);
var top = offset.Y;
var left = offset.X;
TranslateTransform trans = new TranslateTransform();
target.RenderTransform = trans;
DoubleAnimation anim1 = new DoubleAnimation(0, newY - top, TimeSpan.FromSeconds(10));
DoubleAnimation anim2 = new DoubleAnimation(0, newX - left, TimeSpan.FromSeconds(10));
trans.BeginAnimation(TranslateTransform.YProperty, anim1);
trans.BeginAnimation(TranslateTransform.XProperty, anim2);
}
I had to swap two of the values (FROM) with 0. I assume that must be because in this context the upper left corner of the picture is the origin? But now it works.
Please find a solution that uses the Left and Top properties of Canvas for the extension method. See the following code:
I kept having NaN or 0 values for my nested elements, here's a modified version of Danny's answer :
Here it is... It changes the size and moves a
MediaElement
under theCanvas
. Just input your parameters:UPDATE:
Instead of
MediaElement
use this line:And don't forget to put the C# code to:
You can use
MediaElement
as well but you have to define aVideoClip
to see something ;)This code is based on @DeanChalk's answer.
It moves an
Image
contained within aCanvas
(RFID_Token
) diagonally from the top-right to the bottom-left, positioned centrally over anotherImage
within aCanvas
(RFID_Reader
).Try this: