Modelling a menu

Topics: UI Architecture
Jul 28, 2011 at 1:26 PM

I'm investigating the best way to model a menu with MVVM, specifically how to handles user actions based on activating menu items. I'm working with the Telerik RadRibbonView, but this applies to any menu system.

When a user activates a menu item, I want something to happen in the application. That could be any action; opening a screen, saving a document, etc. I don't want to tie all those actions into a MenuViewModel. Should I raise an event using the event aggregator when a menu item is activated? If so, should I have one event for all items or and event per item? ie:

MenuItemActivatedEvent { Name = "Save" }



With the first approach I could have just one method on the MenuViewModel, with the second approach I'd need one method per menu item.

Is raising events the correct way to allow menu functionality?

Jul 29, 2011 at 10:59 PM

You might consider building several MenuCommand classes (OpenSomeScreenCommand, SaveCurrentDocumentCommand), then compose them into some structure (a list or a tree) and bind them to the menu UI using a template
Each command could have an Execute method (either a void or an IEnumerable<IResult> one) bound to the menu click with Message.Attach. 
Plus, the commands' interface could also provide some information about the caption displayed on the menu.

Nov 15, 2011 at 9:53 PM

I'm looking at this again now. Couple of questions:

  • In the open screen command, should I have a direct reference to the conductor so I can add a screen?
  • When the screen requires some data to be loaded, should I do this in the command or in the OnActivate method of the screen?
  • How do I pass data from the command to the screen?



Feb 2, 2012 at 4:14 PM

I'm also interested in how to "correctly" model a menu, this topic seems to crop up fairly regularly so any examples anyone has would be handy. 

Feb 3, 2012 at 3:22 PM

This topic had some solid guidance from Bladewise about how to model a menu.  It's something I have begun to implement myself.