Reply to comment

Menus

Something I got almost, but not quite, figured out is how to do right-click menus. Since I wrote a program with two UIs (WinForms and WPF) I wanted to implement context menus in the viewmodel layer somehow, since the context menus should be the same in both versions. So I came up with an "IMenuItemVM" interface that represents the attributes of a menu item: Text, IsChecked, IsVisible, etc.

Next I made a base class called ContextMenuHub<VM> where VM is a viewmodel class representing the object that was right-clicked. It has an AddMenuItems event; view or viewmodel code can subscribe to this event to pick menu items for a VM when it is right-clicked. It also has a Click event; the view or viewmodel can subscribe to this event to handle when a menu item is clicked. A derived class (e.g. WinFormsContextMenu<VM>) implements the menu-showing behavior. Finally, the presentation model has a ContextMenuHub property (interestingly, the new documentation here does not mention the "presentation model" or "navigation model" anymore... and I didn't really understand the difference between the two anyway.)

Currently I define the menu in the VM layer, placing Click handlers in the view if they need to create or manipulate views, and other Click handlers in the viewmodel.

The main problem with my implementation is that there is no protocol for ordering the menu items or placing separators between them. I am curious if you have ever tried to solve the context-menu problem in the viewmodel layer, or if you think it's a bad idea in the first place.

Reply

By submitting this form, you accept the Mollom privacy policy.