在WPF中如何从后面的代码动画UIEllement的X属性?

时间:2010-09-21 09:55:23

标签: c# wpf animation storyboard

我想从后面的代码中设置矩形x位置的动画(因为x位置仅在运行时确定)。

我有以下代码:

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();

我不知道的是为PropertyPath提供什么?!

1 个答案:

答案 0 :(得分:1)

如果您将它放在画布上,请使用此

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

我会将代码放在Window.Resorces的Xaml中,给它起一个名字x:Name =“da1”,然后只需在代码中调用它

sb.Begin();