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

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

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

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); //应用
}

 

05-04 06:42