Nat153417

Hi,

I wonder why no one ever mentioned or asked how you can do menu merge in WPF For example, you have multiple controls in your screen, you might like to change the menu/toolbar based on which control receives the focus.



Re: Windows Presentation Foundation (WPF) Menu merge

Yi-Lun Luo - MSFT

Hello, if your menusĄŻ basic structures are the same, say, you only want to change one or two MenuItems when you focus on different Controls, then you can add all MenuItems to the same Menu, and set their Visibility respectively. If your menus are completely different, your best bet is to create different Menus, and set Menu.Visibility according to the current focused Control.






Re: Windows Presentation Foundation (WPF) Menu merge

Nat

It's not practical in real life. I would like something that more like MDI menu merging. We should be able to create a menu structure and stick it to a user control. Then when the user control is active, it will merge itself into the main menu structure.





Re: Windows Presentation Foundation (WPF) Menu merge

Joep Beusenberg

I've been looking for a similar thing for a while.

Say I have a 'big' control with a contextmenu. Within this control reside a few smaller controls with each their own contextmenu.

If the user rightclicks on one of the smaller controls (withing the bigger one) i would like to have a contextmenu popping up consisting of both the menuitems for the 'big' control and the items for the 'smaller' control.

I can't/won't just hard-code the comlete menustructure for each smaller control, becouse the 'smaller' controls can be used outside of the 'bigger' control too.

Is there any (neat) way to get this done

Thanks,

Joep





Re: Windows Presentation Foundation (WPF) Menu merge

Bragi

Have you considered using CompositeCollection for this I haven't tried it myself, but I think it should be possible to assign a CompositeCollection containing:

- a binding to the Items(Source) of the parent's contextMenu and

- the new menuItems you want to add.

The idea being that ContextMenu (and Menu for that matter) are also just ItemsControls (The Doc has a very nice blog series going on about this).

Possible problems with this idea:

-objects can't be in 2 different visual trees at the same time, so the collection must contain a duplicate somehow, or (better I think), the contextMenu must make a new container for all the MenuItems.

This could also be solved by not using MenuItems in the collections, but some sort of object that defines the header and icon, so that new containers are created for all the items.

-event handlers (for the menu items) that were assigned to the ContextMenu (so they are handled while bubling up the visual tree). Since the menu items are in a different visual tree, the event handler has to be redifened or redirected, or possibly class event handlers could be used.