How to cause animation closing for WPF ContextMenu?

Does anyone know if animation can be triggered when closing WPF ContextMenu?

I have some code that starts the animation when opening ContextMenu. Animation makes the context menu disappear. I also want animation when ContextMenu is closed, which makes it disappear.

The code that runs the open fade 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);

Animated animations also work with submenu items.

Please note that I also want to run other animations, in addition to fading and fading. For example, I want the context menu to scale out of nothing, so that it looks like bounces.

+5
source share
2 answers

Popup.PopupAnimation, ContextMenu Popup , ContextMenu, .., , . :

  • Popup.PopupAnimation, , ,
  • ContextMenu ContextMenuClosing, .
  • , Shift-F10 .., ContextMenu .

Popup.PopupAnimation

ContextMenuOpened Popup.PopupAnimation , Popup.IsOpen IsOpen , Dispatcher, . TranslateTransform, Popup, .

, , () . , 1/6 , , , , , .

ContextMenu

, ContextMenuClosing , , , .

, DisplatchPriority.Render . , , . ContextMenuOpened. , ContextMenuOpened.

, :

  • ContextMenuOpened Dispatcher.BeginInvoke , .
  • ContextMenuClosed Dispatcher.BeginInvoke , , Child ContextMenu, IsOpen true,
  • ( ), Popup IsOpen false
  • , ContextMenu DataContext , , ContextMenu.

ContextMenu

ContextMenuEventArgs.Handled true ContextMenuOpened, ContextMenu , ContextMenu . :

  • , ( !) ContextMenu
  • Popup.IsOpen true .
  • ContextMenu, , Popup.IsOpen false

, ContextMenu ( 3) . , NET Framework ContextMenu .

+7

All Articles