2

I have a StackPanel on a WPF window, and have bound the Visibility property to another element on the window. The StackPanel appears and disappears correctly, so I thought I would add an animation to it.

I tried the following style, which I applied to the StackPanel...

        <Style x:Key="VisibilityChangedStyle"
           TargetType="{x:Type StackPanel}">
        <Style.Triggers>
            <Trigger Property="Visibility"
                     Value="Visible">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Height"
                                             From="0"
                                             To="30"
                                             Duration="0:0:0.3" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <Trigger Property="Visibility"
                     Value="Collapsed">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Height"
                                             From="30"
                                             To="0"
                                             Duration="0:0:0.3" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Height"
                                             From="0"
                                             To="30"
                                             Duration="0:0:0.3" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>
    </Style>

This works fine when the StackPanel is shown, but when it's hidden, it just disappears without animation.

My guess is that the Visibility property is being set to Collapsed before the animation runs, but I can't work out how to fix this. I'm new to animation, and have spent about two hours reading up, and am thoroughly confused.

Please can someone explain to me the best way to achieve what I want. Thanks

Also, is there a way to use the actual height of the StackPanel instead of hard-coding as I did? I saw someone use a clever technique that bound the To property of the animation to the ActualHeight property of the element, but I got an exception "Cannot freeze this Storyboard timeline tree for use across threads" when I tried that. Anyone know how to bind To to the actual height?

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
  • Did you found a good solution for this? – qakmak Dec 22 '15 at 05:22
  • I binding 2 data trigger bool view model property. one for display all control. another for the main layout in the control. and every time before hide , I hide the main layout first and sleep the thread little time, and hide the control. all operate executed by code behind use MVVM – qakmak Dec 25 '15 at 04:59

1 Answers1

0

See here for question one:

Do something before the visibilty is changed in wpf

and here for question two:

WPF animation: binding to the "To" attribute of storyboard animation

Community
  • 1
  • 1
ndonohoe
  • 9,320
  • 2
  • 18
  • 25
  • Thanks for the links, but I'm not sure how to change the visibility back to Visible before the animations starts. As I said, I'm new at animation, and am finding it very confusing. so many different ways to do things, so few seem to work for me! As for the binding the "To" property, I'll need to spend more time on that one. Thanks again. – Avrohom Yisroel Nov 26 '14 at 18:52
  • Please can you explain how to set the visibility back to visible before the animation starts? I can't work this out at all. – Avrohom Yisroel Nov 29 '14 at 18:45
  • Anyone able to help here? I'm completely stuck. – Avrohom Yisroel Dec 02 '14 at 15:57