Vitalij
Vitalij

Reputation: 4625

In WPF how to animate X property of the UIEllement from the code behind?

I want to animate the rectangle x position from my code behind (as the x position is only determined at the run time).

I have got the following code:

KeySpline easeOut = new KeySpline(0, 1, 0.3, 1);
Storyboard sb = new Storyboard();
DoubleAnimationUsingKeyFrames da1 = new DoubleAnimationUsingKeyFrames();

SplineDoubleKeyFrame keyFrame1 = new SplineDoubleKeyFrame();

GeneralTransform generalTransform = rect4.TransformToVisual(this);
Point point = generalTransform.Transform(new Point());

keyFrame1.Value = point.X;

keyFrame1.KeySpline = easeOut;

da1.KeyFrames.Add(keyFrame1);

sb.Children.Add(da1);

Storyboard.SetTarget(da1, rect);
Storyboard.SetTargetProperty(da1, new PropertyPath("What is the path?"));


sb.Begin();

The thing I don't know is what to put for the PropertyPath?!

Upvotes: 0

Views: 1661

Answers (1)

Lukasz Madon
Lukasz Madon

Reputation: 14994

If you placed it on the Canvas use this

Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.Left)"));

I would place the code in the Xaml in Window.Resorces, give it a name x:Name="da1" and ten just simply call it in code

sb.Begin();

Upvotes: 1

Related Questions