How do I trigger a closing animation for a WPF ContextMenu?
- by Ashley Davis
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.