Region inside a View

Topics: Prism v4 - WPF 4
Mar 3, 2011 at 12:55 PM

Hi,

I am creating WPF app (using Prims+Mef). I have a single region in Shell (Say MainRegion). On Loading a Module A, I am attaching an View One (with toolbar etc.) into the MainRegion.

Now I  have a requirement, such that if I click a button on the View One, it will attach another View (say View Two) into the View One. I am putting some snippets of this,

Shell
<Grid>

    <ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
</Grid>

 

View One
<UserControl>
    <Grid>

    <Button>Add New View</Button>
   <ItemsControl Name="MainPageRegion" cal:RegionManager.RegionName="MainPageRegion" />
  </Grid> 

</UserControl>

 

View Two:

<UserControl>
    <Grid>

   <TextBlock>Hello world</Textblock>
  </Grid> 

</UserControl>

Now I want to attach View Two Inside View One on click of a Button in View One. Is this possible.

Any pointers/links/ideas are appreciated.

Thanks,

Indro
 

Mar 3, 2011 at 1:57 PM

Hi Indro,

Based on my understanding of your scenario, one possible way to achieve your requirement is to use the View Injection Approach. By getting an instance of the RegionManager, so you can add View Two in the same region where View One resides, programmatically. For example it could be something like this:

private void Button_Click(object sender, RoutedEventArgs e)
{
   IRegionManager _regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
   var viewTwo = ServiceLocator.Current.GetInstance<ViewTwo>();

   IRegion mainPageRegion = _regionManager.Regions["MainPageRegion"];

   mainPageRegion.Add(viewTwo);

   mainPageRegion.Activate(viewTwo);
}

 

Additionally, you can read Chapter 7: Composing the User Interface where this topic is covered.

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Mar 7, 2011 at 4:53 AM

Hi Mig,

Thanks for your reply. But I am not able use this code, somehow.

 IRegion mainPageRegion = _regionManager.Regions["MainPageRegion"];

This line is failing, saying unable to find the region.

Is it possible to post the code snippets? Thanks in advance.

 

regards,

Indro

 

 

Developer
Mar 9, 2011 at 3:07 PM
Edited Mar 9, 2011 at 3:08 PM

Hi Indro,

Based on our understanding of the code you've posted, there seems to be a region named MainPageRegion defined inside your View One, so it should be possible to obtain a reference to it by accessing the RegionManager.Regions property.

Since that's not working as expected, it could be helpful if you could provide us with a repro sample (for example uploading it to SkyDrive), so that we can help you clarify the source of this behavior.

Thanks,

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Mar 9, 2011 at 3:59 PM
Edited Mar 9, 2011 at 4:08 PM
GuidoMaliandi wrote:

Hi Indro,

Based on our understanding of the code you've posted, there seems to be a region named MainPageRegion defined inside your View One, so it should be possible to obtain a reference to it by accessing the RegionManager.Regions property.

Since that's not working as expected, it could be helpful if you could provide us with a repro sample (for example uploading it to SkyDrive), so that we can help you clarify the source of this behavior.

Thanks,

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Am I right in thinking that he needs to use a Scoped RegionManager?

E.g.

IRegionManager scopedRegionManager = this.regionManager.Regions["MainRegion"].Add(ViewOne, null, true);
scopedRegionManager.Regions["MainPageRegion"].Add(ViewTwo);

So when you add ViewOne to the MainRegion you need to specify that you want a new region manager in the Add method. This region manager is aware of regions inside ViewOne. With this approach I am not sure which region manager will be injected inside ViewOne code however, the global one or the scoped one?

 




        
    
Developer
Mar 9, 2011 at 5:00 PM

Hi,

In case he has to add more than one instance of ViewOne, he should use a Scoped RegionManager, since there can't be more than one region with the same name within the same instance of the RegionManager. However, since the exception he's experiencing tells him that there is no region associated to that name, the problem he's having probably doesn't relate to the fact that he's not using a scoped RegionManager.

In the example you've provided, the instance of ViewOne will be added to the region belonging to the main RegionManager, but any regions defined inside ViewOne will be registered in the scoped RegionManager.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi