Achieve “slide down” animation in WPF

后端 未结 3 1098
我在风中等你
我在风中等你 2020-12-15 09:33

I am attempting to create my own template for an Expander control. When the control is expanded, I want the content to slide down slowly.

The desired he

3条回答
  •  醉梦人生
    2020-12-15 10:03

    This is kind of an old question but I had problems with this today, so I guess posting my solution would be worth it:

    I had to animate the Height property of a grid row (sliding up and down), but needed dynamic binding so that the row would slide again to the same position as before.

    I found this answer to be very helpful (after fruitlessly battling XAML): http://go4answers.webhost4life.com/Question/found-solution-work-protected-override-190845.aspx

    Sometimes doing things in the code-behind is just simpler:

            Storyboard sb = new Storyboard();
    
            var animation = new GridLengthAnimation
            {
                    Duration = new Duration(500.Milliseconds()),
                    From = this.myGridRow.Height,
                    To = new GridLength(IsGridRowVisible ? GridRowPreviousHeight : 0, GridUnitType.Pixel)
            };
    
            // Set the target of the animation
            Storyboard.SetTarget(animation, this.myGridRow);
            Storyboard.SetTargetProperty(animation, new PropertyPath("Height"));
    
            // Kick the animation off
            sb.Children.Add(animation);
            sb.Begin();
    

    The GridLengthAnimation class can be found here: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/da47a4b8-4d39-4d6e-a570-7dbe51a842e4/

提交回复
热议问题