Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate a StackPanel when the property Visibility changes

In WPF 3.5 (with SP1), I have simply StackPanel that I would like to animate when I change the property Visibility. I have no idea of the height of this StackPanel since its content determines its height. So when I change the property of my StackPanel to Visible (progressPanel.Visibility = Visibility.Visible;) I would like to see an animation (probably an DoubleAnimationUsingKeyFrames from 0 to X).

Moreover, I have multiple StackPanel that I would like to see with this behavior (so in the best case, I need something generic). Does anybody have an idea on how to do that?

Thanks!

like image 841
Martin Avatar asked May 21 '26 12:05

Martin


2 Answers

You can create and reuse custom StackPanel style that triggers animation when Visibility changes:

<Style x:Key="MyStyle" TargetType="{x:Type StackPanel}">
    <Style.Triggers>
        <Trigger Property="Visibility" Value="Visible">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard >
                        <DoubleAnimation .../>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>
like image 53
aku Avatar answered May 23 '26 01:05

aku


If you need an expanding kind of effect with an animation which grows vertically. Do a double animation on the ScaleTransform.ScaleY property of the panel from 0 to 1 if it is a vertical oriented panel.

like image 45
Jobi Joy Avatar answered May 23 '26 02:05

Jobi Joy