同步WPF动画问题

时间:2012-08-24 14:25:13

标签: c# .net wpf

我无法同时同步位置和大小的WPF动画。

请查看我的代码,如果出现问题,请告诉我。

谢谢!

 void AnimatePlugin(double fromTop, double toTop, double fromLeft, double toLeft,
            double fromH, double toH, double fromW, double toW, UIElement control)
        {
            Storyboard sb = new Storyboard();

            #region Top

            DoubleAnimation daTop = new DoubleAnimation();
            daTop.From = fromTop;
            daTop.To = toTop;
            daTop.Duration = new Duration(TimeSpan.FromSeconds(1));
            sb.Children.Add(daTop);
            Storyboard.SetTargetProperty(daTop, new PropertyPath("(Canvas.Top)"));
            Storyboard.SetTarget(daTop, control);
            #endregion

            #region Left

            DoubleAnimation daLeft = new DoubleAnimation();
            daLeft.From = fromLeft;
            daLeft.To = toLeft;
            daLeft.Duration = new Duration(TimeSpan.FromSeconds(1));
            sb.Children.Add(daLeft);
            Storyboard.SetTargetProperty(daLeft, new PropertyPath("(Canvas.Left)"));
            Storyboard.SetTarget(daLeft, control);
            #endregion

            #region Heigh

            DoubleAnimation daH = new DoubleAnimation();
            daH.From = fromH;
            daH.To = toH;
            daH.Duration = new Duration(TimeSpan.FromSeconds(1));
            sb.Children.Add(daH);
            Storyboard.SetTargetProperty(daH, new PropertyPath("(Canvas.Height)"));
            Storyboard.SetTarget(daH, control);
            #endregion

            #region Width

            DoubleAnimation daW = new DoubleAnimation();
            daW.From = fromW;
            daW.To = toW;
            daW.Duration = new Duration(TimeSpan.FromSeconds(1));
            sb.Children.Add(daW);
            Storyboard.SetTargetProperty(daW, new PropertyPath("(Canvas.Width)"));
            Storyboard.SetTarget(daW, control);
            #endregion

            sb.Completed += (w, r) => { control.UpdateLayout(); };
            sb.Begin();
        }

1 个答案:

答案 0 :(得分:1)

只有Canvas.LeftCanvas.Top是附加属性....(并且您使用括号正确指定它们)。

但是,“宽度”和“高度”不是附加属性,它们是FrameworkElement的基本属性......只需使用PropertyPath("Width")PropertyPath("Height")

不使用字符串来指定属性的路径,而是可以使用采用DependencyProperty类型的PropertyPath .....所以使用PropertyPath(Canvas.TopProperty)PropertyPath(Canvas.LeftProperty)PropertyPath(Canvas.WidthProperty)PropertyPath(Canvas.HeightProperty)

这样您就不必担心使用PropertyPath字符串的正确语法,这取决于是否附加了属性。