我创建了一个简单的动画,可以在单击按钮时显示我的网格 这个想法是使用窗口大小的实际高度,并制作从0到实际高度的动画 但由于用户将调整窗口大小,我想将高度从实际高度改回Auto 然而,grid1的高度无法修改(不仅仅是自动,即使像600这样的确切值也不起作用)动画完成后,谁都知道为什么?谢谢
XAML
<Grid Name="grid_Main">
<Grid Background="Aqua" Height="0" HorizontalAlignment="Left" Margin="68,39,0,10" Name="grid1" VerticalAlignment="Top" Width="361" >
<ScrollViewer>
<Border Height="200" />
</ScrollViewer>
</Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="214,258,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
CS
private void button1_Click(object sender, RoutedEventArgs e)
{
double actualHeight = grid_Main.ActualHeight;//grid_Main.RowDefinitions[1].ActualHeight;
DoubleAnimation animation = new DoubleAnimation(0, actualHeight, TimeSpan.FromSeconds(1));
animation.Completed += new EventHandler(animation_Completed);
grid1.BeginAnimation(Rectangle.HeightProperty, animation);
}
void animation_Completed(object sender, EventArgs e)
{
grid1.Height = Double.NaN;
grid1.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
grid1.Width = 200;
}
答案 0 :(得分:0)
您需要在animation_Completed的开头清除现有动画:
grid1.BeginAnimation(Rectangle.HeightProperty, null);
在设置动画之前,你也应该养成这样做的习惯,以确保你不会忘记其他可能仍然被绑定到属性的动画,即使它们不再播放。