Question

Suppress default context menu

Posted 6 years ago by Avatar RS
I'm having trouble figuring out how to prevent the default context menu from appearing.

By the time the ContextMenuOpening event is raised, .Handled is already set to True. I've tried defining an empty context menu too - but still the default one gets shown.

Short of handling PreviewMouseRightButtonUp is there a correct way to implement a custom context menu?
RS

Comments (6)

Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Richard,

SyntaxEditor overrides the OnContextMenuOpening method. You could override that and not call the base method.

Alternatively, our implementation of the method calls the protected virtual CreateDefaultContextMenu method. You could override that and return a custom one if you wish.

Actipro Software Support
Posted 6 years ago by RS
That's great if you are inheriting from SyntaxEditor, but what if you are just including it as-is in a Window or other control? Would be nice to be able to set .Handled to True in the ContextMenuOpening event (or something similar) to cancel the default context menu at least without having to subclass the editor.
RS
Posted 6 years ago by RS
Just a suggestion, but surely if I specify a ContextMenu on SyntaxEditor (in Xaml or code) then that should be used in place of the default one?
RS
Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Where we are overriding that method is the last place we can check to see if we need to display our own. We call the base method before our code is started too, but since that method is raised internally in Microsoft's code by the bubble handler before any other attached ContextMenuOpening events are raised, your handler occurs after our code.

You are right, we should check for whether a ContextMenu is already assigned. We'll add code for that.

Think we should add a bool property for whether to generate the default menu too? That would be the only workaround for your original question I can think of.

Actipro Software Support
Posted 6 years ago by RS
The odd thing from a regular Wpf point of view is that setting .Handled in the event doesn't prevent the context menu from showing (it would do normally) - I guess that's because you are generating the context menu on demand and therefore the code path to show the context menu isn't handled by Wpf?

This standard way to supress a context menu by setting .Handled (as documented at the bottom of http://msdn.microsoft.com/en-us/library/bb613568.aspx) would be the ideal situation IMHO if it's at all possible as it allows any event listener to prevent the context menu from appearing. And it's perfect for us, as we actually forward the context menu handling to a parent control.

If that's simply not possible because of the event ordering etc. then I think it would definitely be useful to somehow identify that there is no context menu. I tried setting ContextMenu to nothing (as I expected the default context menu to be returned by the ContextMenu property) - that would be the most obvious thing a developer would try I think.

An IsDefaultContextMenu property would be just as usable but perhaps not as intuitive - but your finished docs are pretty spot on in pointing out those kind of hard to spot properties so that's probably not too much of an issue.
RS
Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
We found another way to set the default context menu properly in XAML (we had an issue before) so this should work as expected now. Setting ContextMenu = null will clear it.

Actipro Software Support
Information The latest build of this product (2014.2 build 0610) was released 5 days ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.