Unable to find PRISM regions defined inside RadExpander content

Topics: Prism v4 - Silverlight 4
Sep 23, 2011 at 10:06 AM

Hi,

I am placing a region "JobTabRegion"(using a ContentControlin the DataTemplate of  a RadTabControl in my Shell. This "JobTabRegion" is bind to view named "MainJobView" which in turn has a RadTabControl and regions are marked inside the RadTabItem.Content (using a ContentControlof each RadTabItem. One such region would be "JobRequestRegion" which is bind to a view named "JobRequestView". This "JobRequestView" has a RadExpander control and I am having a region marked (using a ContentControlinside the RadExpander.Content called "ExpanderRegion" . I am using two buttons to switch the contents of the RadExpander. To do so, in the ViewModel of  "JobRequestView", when i try to access the "ExpanderRegion" from the Region Manager instance, i get an error saying "The region manager does not contain the ExpanderRegion region." 

var region = _regionManager.Regions[RegionNames.EXPANDERREGION];


I have read in an article that the regions marked inside a DataTemplate will not be available in the RegionCollection of the Region Manager. But i didn't get any proper solution. I am using PRISM v 4 andSilverlight 4.

Any help would be appreciated. Thanks in advance.

Syam

Developer
Sep 23, 2011 at 7:45 PM

Hi Syam,

As you have mentioned, this is a known issue in Prism where, if a region is defined inside a DataTemplate, the region is created but not registered in the RegionManager.

You can find a possible workaround for this in the aforementioned work item or in the following blog post:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Sep 24, 2011 at 6:08 AM

Hi DCherubini,

Thanks for your reply.

In my requirement, i need to use View Injection as i have to switch views in the same region. In your blog post, you had mentioned that 'Attaching the RegionManager in the DataTemplate as a property' solution doesn't work properly in Silverlight. Is there any alternative for this in Silverlight as I am facing this issue in Silverlight.

 

Thanks

Syam

Developer
Sep 26, 2011 at 2:59 PM
Edited Sep 26, 2011 at 2:59 PM

Hi,

Based on my understanding, there is no problem when adding the RegionManager as an Attached Property in the region defined inside a data template in Silverlight. That is to say, the main idea of the blog post, which is to make the FindRegionManager method in the RegionManagerRegistrationBehavior find an actual region manager in regions defined inside data templates, is still valid in Silverlight; so it can be used as a workaround to avoid these regions not being found in the correct region manager.

However, it is equally important to know which region manager will be attached into that region (especially if you have a scenario when you need to use scoped regions.) The workaround mentioned in the blog post (which is to define a dynamic resource that is updated when the view containing the data template is added into the visual tree) won't be possible to implement, because Silverlight doesn't provide OnVisualParentChanged method.

Therefore you should follow a different approach to obtain the appropriate region manager. One possible way to achieve this could be to define your ViewModel as a resource in your view, so you can bind to its properties from within your data template, which would look like this (assuming you have defined a resource named "ViewModel" in your view, and a property named RegionManager in your view model):

 <ItemsControl prism:RegionManager.RegionManager="{Binding RegionManager, Source={StaticResource ViewModel}}" prism:RegionManager.RegionName="RegionInsideDataTemplate"></ItemsControl>

If you do this, please take into account that you need to define a proper logic to obtain the appropiate RegionManager in your view model, especially if you are using Scoped Regions. For example, you could define a shared service responsible for obtaining the correct region manager based on certain criteria that you define, or you could simply obtain the main region manager from the container if you are sure you won't be using scoped regions in that case.

I hope you find this helpful.

Agustin Adami
http://blogs.southworks.net/aadami

Sep 28, 2011 at 2:44 AM

Hi Agustin,

 

Thanks for your reply. I will try this and let you know.