views:

104

answers:

1

I am creating a menu that will have its menu items injected into it from different modules (I am using the Composite Application Library: Unity, Prism).

Example:

File
- Open
- New
  - A (injected from ModuleA)
  - B (injected from ModuleB)
  - C (injected from ModuleC)
- Exit

Because I don't know which items will be injected, I need to somehow sort my Menu Items. I am trying to do this now by creating a RankedMenuItem that inherits from MenuItem. RankedMenuItem has one additional property, Rank, which is an integer.

I set the "File -> Exit" menu item to be rank 1000, and "File -> Open" to be rank 0, so that Exit will always show up at the bottom of the File menu, and Open will always show up at the top.

The trouble I'm having is that I can't figure out the best way to sort the menu hierarchy. I can sort the top level easily using a CollectionView, but I can't figure out how to apply it to the subitems.

Any advice would be much appreciated.

A: 

By default the MenuItem.ItemsSource can contain any collection-based set. This can be a sorted list, for example, or an collectionviewsource. If the items in the collection are domain-specific (e.g. not MenuItem objects) then your task may be less-than-trivial. Based on your description it sounds as though your modules register a simple MenuItem object directly into the Items of the "New" MenuItem, thus making it impossible to perform a sort. If that's the case, my suggestion is to have something like ObversableCollection which items are added/removed via, and use that via a CollectionViewSource attached to the MenuItems property of the "New" menu-item. This should allow you to sort menu items added dynamically by other modules (though, how you implement the sort is up to you, e.g. requiring a RankedMenuItem from the modules, or you could simply sort alphabetically with some product-specific "weighting" to ensure some menu items are always sorted above/below other "known" menu items.)

Hope that helps.

Shaun Wilson
We implemented our solution in a similar fashion. Sorry for the late accept.
Elmo Gallen