WPF编程,通过Double Animation动态更改控件属性的一种方法。

版权声明:我不生产代码,我只是代码的搬运工。 https://blog.csdn.net/qq_43307934/article/details/87251422

DoubleAnimation类指定起始值(From="30”)、终点值(To="300")、时间(Duration=“3"),以及动画结束应该如何(FillBehavior=“Stop”)。

设置好后该矩形调用BeginAnimation 方法开始实现动画,BeginAnimation 指定需要应用动画的属性(注意这里传入的必须是依赖属性)和创建的DoubleAnimation。 

这里动态更改一个方块的宽度。 

1、前台

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height=" *" />
            <RowDefinition Height=" *" />
        </Grid.RowDefinitions>
        <Rectangle Name="rectan"
                   Width=" 30"
                   Height=" 100"
                   Fill="Blue" />
        <Button Grid.Row=" 1"
                Width=" 100"
                Height=" 30"
                Click="Button_Click" />
    </Grid>

2、后台

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation doubleanimation = new DoubleAnimation();
            doubleanimation.From = 30; //初始宽度
            doubleanimation.To = 300;  //目标宽度
            doubleanimation.Duration = TimeSpan.FromSeconds(3);  //耗时
            doubleanimation.FillBehavior = FillBehavior.HoldEnd; //结束后的动作
            rectan.BeginAnimation(Rectangle.WidthProperty, doubleanimation);  //应用
        }

猜你喜欢

转载自blog.csdn.net/qq_43307934/article/details/87251422