WPF中从旧值到新值的平滑过渡

WPF中从旧值到新值的平滑过渡,wpf,properties,transform,transition,smooth,Wpf,Properties,Transform,Transition,Smooth,在窗口中是一个具有一定大小的矩形,我需要为该矩形设置一个新的大小,逐渐将其旧大小更改为新大小。或者,例如,必须将其平滑的矩形旋转。怎么做 编辑 我想我表达得不好。 例如,我有一个尺寸为200 x 300的矩形。我试着给它添加新的维度:400乘200,我希望他不要快速应用新的、平滑的动画命中目标值 我想这在WPF中是如何实现的?我不知道你所说的将矩形变平滑是什么意思 故事板。此示例甚至显示如何调整矩形的大小 老问题,但我有同样的问题并找到了解决方案 我认为用xaml是不可能的,只能用代码 usi

在窗口中是一个具有一定大小的矩形,我需要为该矩形设置一个新的大小,逐渐将其旧大小更改为新大小。或者,例如,必须将其平滑的矩形旋转。怎么做

编辑

我想我表达得不好。 例如,我有一个尺寸为200 x 300的矩形。我试着给它添加新的维度:400乘200,我希望他不要快速应用新的、平滑的动画命中目标值


我想这在WPF中是如何实现的?我不知道你所说的将矩形变平滑是什么意思

故事板。此示例甚至显示如何调整矩形的大小


老问题,但我有同样的问题并找到了解决方案

我认为用xaml是不可能的,只能用代码

using System.Windows.Media.Animation; // this is needed for use DoubleAnimation in code

...

private void ResizeRectangle(double width, double height)
{
    // Height
    DoubleAnimation a = new DoubleAnimation();
    a.From = MyRectangle.ActualHeight; // animation start with actual height
    a.To = height; // desired height
    a.Duration = new Duration(TimeSpan.FromSeconds(1)); // duration for From to To (you can use miliseconds), this make smooth transition from the old to the new values
    a.FillBehavior = FillBehavior.Stop; // FillBehavior should be stop, if you want do another size changes
    MyRectangle.BeginAnimation(Rectangle.HeightProperty, a);  // set up animation for MyRectangle

    MyRectangle.Height = height; // don't forget change size for rectangle, because FillBehavior.Stop stop animation and it set back to actual size

    // Width
    DoubleAnimation b = new DoubleAnimation();
    b.From = MyRectangle.ActualWidth;
    b.To = width;
    b.Duration = new Duration(TimeSpan.FromSeconds(1));
    b.FillBehavior = FillBehavior.Stop;
    MyRectangle.BeginAnimation(Rectangle.WidthProperty, b);

    MyRectangle.Width = width;
}

这就是链接中的示例所做的。你试过了吗?我不知道我怎么能用它。我更改此设置:retangle1.Size=新大小(400200);我需要在动画中应用新的设置。所以你没有尝试,因为这正是故事板和双动画所做的。这不是新的尺寸总是任意的,sledovatelnoi旧的任意。私有无效按钮\u单击(对象发送器,路由目标){MyRectangle.Height=400;MyRectangle.Width=300;}
using System.Windows.Media.Animation; // this is needed for use DoubleAnimation in code

...

private void ResizeRectangle(double width, double height)
{
    // Height
    DoubleAnimation a = new DoubleAnimation();
    a.From = MyRectangle.ActualHeight; // animation start with actual height
    a.To = height; // desired height
    a.Duration = new Duration(TimeSpan.FromSeconds(1)); // duration for From to To (you can use miliseconds), this make smooth transition from the old to the new values
    a.FillBehavior = FillBehavior.Stop; // FillBehavior should be stop, if you want do another size changes
    MyRectangle.BeginAnimation(Rectangle.HeightProperty, a);  // set up animation for MyRectangle

    MyRectangle.Height = height; // don't forget change size for rectangle, because FillBehavior.Stop stop animation and it set back to actual size

    // Width
    DoubleAnimation b = new DoubleAnimation();
    b.From = MyRectangle.ActualWidth;
    b.To = width;
    b.Duration = new Duration(TimeSpan.FromSeconds(1));
    b.FillBehavior = FillBehavior.Stop;
    MyRectangle.BeginAnimation(Rectangle.WidthProperty, b);

    MyRectangle.Width = width;
}