//指定长度变化的起点,终点与持续时间,并在动画结束时恢复原值 DoubleAnimation widthAnimation = new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.Stop); //指定高度变化的起点,终点与持续时间,并在动画结束时恢复原值 DoubleAnimation heightAnimation = new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.Stop);
以下是完整的实例代码:
using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Windows.Media.Animation; namespace DoubleAnimationTest { /**////<summary> /// Interaction logic for Window1.xaml ///</summary> public partial class Window1 : System.Windows.Window { private Grid gridRoot; private Button buttonTest; public Window1() { IniComponent(); } privatevoid IniComponent() { this.gridRoot =new Grid(); this.buttonTest =new Button(); this.buttonTest.Content ="this is a test button"; this.buttonTest.Width =200; this.buttonTest.Height =50; this.buttonTest.Click +=new RoutedEventHandler(buttonTest_Click); this.gridRoot.Children.Add(this.buttonTest); this.Content = gridRoot; } void buttonTest_Click(object sender, RoutedEventArgs e) { Button btn = sender as Button; //指定长度变化的起点,终点与持续时间,并在动画结束时保持大小 DoubleAnimation widthAnimation = new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.HoldEnd); //指定高度变化的起点,终点与持续时间,并在动画结束时保持大小 DoubleAnimation heightAnimation = new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.HoldEnd); //开始动画 //变化不是阻塞的,而是异步,所以看上去长度与高度几乎是同时变化 btn.BeginAnimation(Button.WidthProperty, widthAnimation); btn.BeginAnimation(Button.HeightProperty, heightAnimation); } } publicclass MainClass { [STAThread] publicstaticvoid Main() { Window1 win =new Window1(); Application app =new Application(); app.Run(win); } } }