1
votes

I created a simple animation to show my grid when click on a button the idea is using the actual height of window size and make an animation from 0 to actual height but since user will then resize the window, I would like to change the height from actual height back to Auto However, the height of grid1 could not be modified (not just auto, even an exact value like 600 is not working) afer animation completed, anyone knows why? thank you

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;
    }
1

1 Answers

0
votes

You need to clear the existing animation at the start of animation_Completed:

grid1.BeginAnimation(Rectangle.HeightProperty, null);

You should also get into the habit of doing this before you set your animation to make sure you don't wind up with memory leaks from other animations that may still be bound to the property even though they're no longer playing.