views:

35

answers:

1

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?!

A: 

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();
lukas
But it is not inside of the canvas. As I have already mentioned, I only determine the X position during application runtime, I could possible change the keyFrame value during runtime, but with this approach I would have code in scattered around my application, which I don't want to have.
Vitalij
How do you determine this? If you place an object inside Grid, StackPanel etc it won't have coordinates. For silverlight I use (UIElement.RenderTransform).(CompositeTransform.TranslateX) which I belive you need to change to TranslateTransform (also it needs to be alocated "new TranslateTransform"). It uses relative values.
lukas