Regions and Resource Dictionary assets

Topics: Prism v2 - WPF 3.5
May 19, 2009 at 11:57 PM

The approach I've been taking for adding elements such as a MenuItem to an IRegion has been to declare the MenuItem is a module level resource file (e.g. Resources\MenuResources.xaml).  I then pull the element out of a ResourceDictionary myself and add it to the desired region.  I can't remember if I picked up this technique from the early bits of Prism or not, but such an approach doesn't seem to be shown in the current examples.

What is the perscribed practice for doing something like this if other than this approach?

May 20, 2009 at 7:59 PM
Edited May 20, 2009 at 7:59 PM

Hi Derek,


I have thought of two possible approaches for the scenario you just described.


Scenario 1

If your menu belongs to a specific module, you could just create a view through XAML in that module, that contains your menu items. This is because no other items will be added from other modules, so there is no need to attach a region  to the menu. For this scenario WPF DataTemplates might be a good option. There is a really good sample from Julian Dominguez (prism-v1 and v2 developer) that shows how to use Presentation Models with DataTemplates in WPF (includes source code): Presentation Model with DataTemplates in CompositeWPF (Prism) Sample.


Scenario 2

If your menu contains items from different modules you could use UI Composition. In Prism-v2 regions are the placeholders that will display one or more views at runtime, therefore allowing UI Composition.

Regions are usually populated with views (that are in modules). The most common approach is defining views in modules, and adding them to regions.


So if you want to add MenuItems to a region, you could create different views in your modules (each could contain just a menu item) and add them to a MenuItemRegion.


The following articles provide more information about the topics I mentioned above:

·         How to: Show a View in a Region Using View Discovery UI Composition

·         How to: Show a View in a Region Using View Injection UI Composition

·         How to: Create a View with a Presenter


Please let me know if this helps.


Damian Schenkelman

May 27, 2009 at 7:26 PM

The approach you described in scenario 2 is what I indicated I'm currently doing.  My question really is whether newing up a ResourceDictionary to obtain resources which need to be added to some region is the perscribed way of composing elements such as menu items or whether people have coalesced around another approach.