views:

66

answers:

0

Hi all

I created a fade in / fade out effect for the WPF DataGrid´s DetailsView animating the DetailsView´s Opacity and Height properties. The fade in effect works nice but the fade out effect just produces a hard collapse of the DetailsView.

The animations are called in the DataGrid´s RowDetailsVisibilityChanged event handler - I think this is the reason for the missing fade out animation. The DetailsView´s visibility is collapsed before the animations can hit.

But I have no idea how to avoid this hard collapse of the DetailsView´s visibility to produce a nice fade out animation. Hopefully someone here has some advice to give.

Many thanks in advance

banzai

Just to make sure - here is the code that is called in the mentioned event handler:

        public static void RowDetailsAnimation(this DataGrid dg, DataGridRowDetailsEventArgs e)
    {
        if (e.DetailsElement != null)
        {
            DataGridRow row = (DataGridRow)e.Row;
            DataGridDetailsPresenter presenter = (DataGridDetailsPresenter)VisualTreeHelper.GetParent(e.DetailsElement);
            DoubleAnimation fadeAnimation = new DoubleAnimation();
            DoubleAnimationUsingKeyFrames scrollAnimation = new DoubleAnimationUsingKeyFrames();
            Duration duration = new Duration(TimeSpan.FromMilliseconds(500));

            fadeAnimation.Duration = duration;
            scrollAnimation.Duration = duration;
            // Call to Measure is necessary to initialize DesiredSize.Height - otherwise DesiredSize.Height of DetailsElement and its parent container is 0
            e.DetailsElement.Measure(new Size(5000, 5000));
            if (row.DetailsVisibility == Visibility.Visible)
            {
                fadeAnimation.From = 0.0;
                fadeAnimation.To = 1.0;
                scrollAnimation.KeyFrames.Add(new EasingDoubleKeyFrame() { Value = 0, KeyTime = TimeSpan.FromMilliseconds(0), EasingFunction = new ExponentialEase() });
                scrollAnimation.KeyFrames.Add(new EasingDoubleKeyFrame() { Value = e.DetailsElement.DesiredSize.Height, KeyTime = TimeSpan.FromMilliseconds(500), EasingFunction = new ExponentialEase() });
            }
            else
            {
                // Slide and fade out doesn´t work because DetailsView.Visibility is collapsed before animation can hit
                presenter.Opacity = 0.0;
            }
            presenter.BeginAnimation(FrameworkElement.OpacityProperty, fadeAnimation);
            presenter.BeginAnimation(FrameworkElement.HeightProperty, scrollAnimation);
        }
    }
}