Razvan
Razvan

Reputation: 51

WPF TranslateTransform

Im trying to use the TranslateTransform class to move a image on a Grid on Y axis. I need this movment to be smooth so i can not use SetMargin or SetCanvas. I try this in code behind:

public void MoveTo(Image target, double oldY, double newY)
{
    var trans = new TranslateTransform();
    var anim2 = new DoubleAnimation(0, newY, TimeSpan.FromSeconds(2))
                    {EasingFunction = new SineEase()};
    target.RenderTransform = trans;
    trans.BeginAnimation(TranslateTransform.YProperty, anim2);
}

The object i want to use (a Image control) is placed on a Grid. For the first time everything works fine. The problems comes when i try to move the object again using the same function. The object (a Image control) first move to the start position (initial Y coordinate) then the animation begins.

Is it not suposed for the TranslateTransform to change the coordinates (in my case the Margin property) too?

Thank you.

Upvotes: 2

Views: 10601

Answers (4)

Ark-kun
Ark-kun

Reputation: 6787

You've explicitly told the animation to start from 0. It's doing what you've told it. Just remove the explicit zero fromvalue and everything will work.

var anim2 = new DoubleAnimation(newY, TimeSpan.FromSeconds(2)) 
                { EasingFunction = new SineEase() };

Upvotes: 0

diexsie
diexsie

Reputation: 77

You have to use the By property of DoubleAnimation. Try that:

//everytime you execute this anmation your object will be moved 2.0 further
double offset = 2.0 
var anim2 = new DoubleAnimation(newY, TimeSpan.FromSeconds(2));
anim2.To = null;
anim2.By = offset;

Upvotes: 0

tam
tam

Reputation: 1583

The transform does not change the original values.they are your point of origin. If you want a new point of origin each time you move you can handle the animation completed event. Or from your transform you can get your current offset and make that your new start point for the animation.

In other words your start values would always be your last move to values

Upvotes: 1

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19339

The TranslateTransform is a specific kind of render transformation. Rather that changing properties of the control (such as the Margin property), it simply affects how the control is displayed on the screen.

Upvotes: 0

Related Questions