views:

1170

answers:

2

N.B THIS QUESTION HAS BEEN UPDATED, READ FURTHER DOWN

Hi,

I want to create a custom context menu that has 4 sub-menus, each in their own quadrant (top left, top right, bottom left, bottom right). Similar to 3ds Studio Max.

This is how I've approached the issue so far: - create a custom WPF control, derive from ContextMenu[1]
- declare 4 dependency properties of ContextMenu, these will be menus displayed and can be set from XAML.
- AddOwner to the ContextMenu.IsOpenProperty, adding a property changed notification.
- when the IsOpen property changes set the IsOpen property of 4 child context menus.
- using ContextMenuService set the Vertical and Horizontal offsets of the context menus to make them appear in each quadrant; binding the actual height and width properties to calculate the offsets.

[1] Need to derive from context menu otherwise you cannot assign it to the ContextMenu property on the Window.

This appears to work, there are issues with the menus NOT staying open (they're being closed as the focus is outside the menu) but I'm sure using Reflector.NET find a way around this.

This is my first custom WPF control that I've attempted to write; and not sure if this is best approach.

Any suggestions/ideas on how to create this Quad Context menu?

+2  A: 

I can think of two approaches, neither one are necessarily that clean but has the potential to work if you have the time.

Approach 1

Use a context menu but through setting the Template make it so that the ContextMenu really just shows a control that happens to open other windows. Perhaps tricks can be done so that opening these secondary menus doesn't take the focus away. This might end up still causing the original problem.

Approach 2

What happens if you set ContextMenu.StaysOpen to false and then try to control when the menu closes yourself?

Steven
Hey Steven, thanks for you answer it made me think about it little differently. I have updated the post with my latest attempt which is working except for a few new issues. :)
Dennis Roche
Any kind of tweaking outside of the default control behavior is always painful. I think you'd be better off in general if you didn't use the ContextMenu to do it and just went with your own windows. I'd actually make it a behavior (http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx)
Steven
It is _very_ painful overriding the default behavior of a control with doing some hackey™ via reflection to change internal/private properties. I will experiment to see if I can create the same control using attached behaviour, will post back if I have any success. Thanks Steve!
Dennis Roche
Thinking about it RIGHT now I think would be possible via Attach Properties to link several context menus that to open/close at the same time and manage each as a separate resource, as that is really the only unique behavior I need. Hmmm. Something to think about.
Dennis Roche
A: 
Dennis Roche