Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable WPF ContextMenu animations?

Tags:

wpf

I'm trying to hunt down what bit I need to tweak to get ContextMenus in WPF to stop animating when they appear/disappear.

From what I can tell, WPF creates a Popup to host the ContextMenu. The Popup looks at its PopupAnimation property and decides how to animate. What I want to do is always have that set to "None".

I've tried setting a global unnamed style with a TargetType of Popup that sets PopupAnimation to None but this does not work. If I break in System.Windows.Controls.Primitives.Popup.SetupAnimations I can see that the animation type is still set to Fade. I'm guessing that it hasn't had a chance to apply styles yet..

I've tried hooking ContextMenuOpening but there's no access to a Popup in there that I could find.

What else could I try?

Note that this is sort of the second part of another question I asked here. The advice there worked great for menus and everything else we had that was animating, but the one exception has been ContextMenus. They animate based on properties in code, not a template. I verified this by pulling the ContextMenu template out using the advice given here.

like image 922
scobi Avatar asked Jul 01 '10 18:07

scobi


2 Answers

I've been struggling with this too. I've found that the solution is to "override" the system parameter that control popup animation.

Do this by defining a resource (perhaps in your Themes\Generic.xaml) like this:

<PopupAnimation x:Key="{x:Static SystemParameters.MenuPopupAnimationKey}">None</PopupAnimation>
like image 66
Mårten Wikström Avatar answered Nov 05 '22 02:11

Mårten Wikström


I spent a good half hour trying to figure out how to do this in code - I'm sure its obvious if you know the framework better:

var app = new Application();
app.Resources.Add(SystemParameters.MenuPopupAnimationKey, PopupAnimation.None);
app.Run(myThing);
like image 24
AnonDev Avatar answered Nov 05 '22 03:11

AnonDev