How do I trigger a closing animation for a WPF ContextMenu?

Posted by Ashley Davis on Stack Overflow See other posts from Stack Overflow or by Ashley Davis
Published on 2010-06-04T09:25:24Z Indexed on 2010/06/10 10:42 UTC
Read the original article Hit count: 247

Filed under:
|

Does anyone know if it is possible to trigger an animation when a WPF ContextMenu closes?

I have code that triggers an animation when the ContextMenu is opened. The animation makes the context menu fade into view. I also want an animation when the ContextMenu is closed that makes it fade out.

The code that starts the opened fade-in animation looks something like this:

        var animation = new DoubleAnimation();
        animation.From = 0;
        animation.To = 1;
        animation.Duration = TimeSpan.FromSeconds(0.2);
        animation.Freeze();

        menu.BeginAnimation(ContextMenu.OpacityProperty, animation);

The fade-in animation also runs on sub-menu items.

Note that I also want to run other animations besides fade in and fade out. Eg I want the context menu to scale up from nothing so that it sort of 'bounces' into view.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf