0

我不能同时同步位置和大小的 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();
        }
4

1 回答 1

1

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

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

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

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

于 2012-08-24T14:37:31.227 回答