Skip Navigation LinksHome > View Post

Nesting Regions (or creating fractals) with Prism

A number of customers working with Prism (or CAG/CAL) are concerned that regions won't be able to support their requirements with respect to nesting and multiple similar views. The good news is that Prism's Region support can absolutely support this. And to prove it, I've put together an example where the a view, pushes another copy of itself into, errr, itself.

So our Shell has one main Region (the whole visible window) called "Main".

I've added a module (called PrimaryModule) with a single View called FractalView that looks like this:

Fractal View

As you can see, it has three areas with red, yellow and blue borders respectively. The blue border contains a region (also called "Main", just to make things harder). The first thing this module has to do is insert this View into the Shell's "Main" region.

public void Initialize()
{
FractalView fv = new FractalView();
_regionManager.Regions["Main"].Add(fv, null);
_regionManager.Regions["Main"].Activate(fv);
}

However, this would error as the RegionManager would look to register all of FractalView's regions and error due to a collision between FractalView's "Main" region and the Shell's "Main" region. This is where we need to create a scoped Region.

public void Initialize()
{
    FractalView fv = new FractalView();
    RegionManager rm = _regionManager.Regions["Main"].Add(fv, null, true);
    _regionManager.Regions["Main"].Activate(fv);
    fv.SetRegionManager(rm);
}

In this instance we pass an extra parameter to the Region's Add method that specifies true, meaning we would like to create a new region scope. Note that the new RegionManager is returned by this method for us to hold and store, if necessary, for use later. Note that we pass the new RegionManager into the view itself with its SetRegionManager method.

Shell and Fractal View

Our FractalView has a "Add another view..." button that will then inject a new FractalView view into the current FractalView's "Main" region. Cool. Here's my click handler:

// Deja-vu??
private void Button_Click(object sender, RoutedEventArgs e)
{
    FractalView fv = new FractalView();
    RegionManager rm = _regionManager.Regions["Main"].Add(fv, null, true);
    _regionManager.Regions["Main"].Activate(fv);
    fv.SetRegionManager(rm);
}

You can now click the button and, wahey! You can click another one... and another one... and another one... until:

Shell and Fractal Viewsssssss

Not the most performant way to create fractals, but you get the point.

Tags: Prism WPF

 
Josh Post By Josh Twist
10:41 AM
03 Dec 2008

» Next Post: Controllerizing the ScrollViewer Thumbnail
« Previous Post: WPF ScrollViewer Thumbnail

Comments are closed for this post.

Posted by Inferis @ 03 Dec 2008 3:49 PM
I use this a lot, but I think it stinks that the code constructing and constructing the view must take of this.
I think it would make a lot more sense if you can have a view say: hey, I'm a scoped region than having to do it externally.

The caller creating the view shouldn't care about scoped regions or not: what goes on inside the view and how is none of it's business.

Posted by Inferis @ 03 Dec 2008 3:50 PM
Well, that should have been "constructing and activating" instead of a double "constructing". That's what you get posting after midnight.

Posted by Francisco @ 30 Dec 2009 10:51 AM
Hello, this is a very nice way to explain the concept.

I do have one question regarding the following.

fv.SetRegionManager(rm);

This method only stores the regionManager in the view, right? Or is it a CAL framework interface method ?

Btw, can you provide have the source ? Thank you.

Posted by Atena Megano @ 10 Jul 2011 9:21 PM
Can you give us the sample source code, PLEASE.

© 2005 - 2014 Josh Twist - All Rights Reserved.