Dynamically create regions

Topics: Prism v2 - WPF 3.5
May 1, 2009 at 4:52 PM
I am still in the 'trying-out' stage and I have a question at the moment.
While the program is running, can I create a panel (eg on a button-click event), let the panel be a region, and then load a module into it??
If yes, any pointers in that direction will be highly appreciated. Thanks in advance.
May 4, 2009 at 8:48 PM

Hi Osei,


It's great that you are trying Prism-v2!

Creating regions dynamically is perfectly supported and there are several ways you can achieve it depending on your scenario. Once the region is added, you can register views into it.

1.       The most usual way is having the panel as a separate view  (as long with its resources) and attach a region to it in xaml code as usual:
<ItemsControl cal:RegionManager.RegionName="yourRegionName" />

Then all you have to do, is dynamically adding this view to some other region using view injection.

2.       If for some special reason you want to do this by code, you can create a new instance of the panel, and attach a region to it as follows:

RegionManager.SetRegionName(yourPanel, "yourRegionName");

3.       In some cases, for example if you need to create several instances of the panel, you can use Scoped Regions so that each view gets its own RegionManager and its regions will be registered with that RegionManager instead of with the parent RegionManager


To load modules dynamically you might check: How to: Load Modules on Demand


You might also find useful the following articles from the documentation:

·         How to: Add a Region

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

·         How to: Show a View in a Scoped Region

·         UI Composition (Technical Concept)

·         UI Composition (Design Concept)


Hope it helps!


Matias Bonaventura