views:

469

answers:

4

Hi,

Does anyone know how to get the control associated with a region in Prism.

For example, if I have the following code to register a region

RegionManager.SetRegionName(Outlook.navigationContainer, "navigationContainer");

How can I get the control Outlook.navigationContainer via the name "navigationContainer"?

Thanks heaps.

A: 

I don't think you can easily get the associated control via the region name.

The SetRegionName method only sets the "RegionNameProperty" DependencyProperty on the specified control. The only thing you can do is to get the name back when you have the control.

Mischa
+1  A: 

Regions are supposed to abstract away the specifics of the region control from you. Why do you need to get the control?

HTH, Kent

Kent Boogaart
Hi Kent,I agree with what you are saying, however, what if I have complex functionality such as modifying a property on the region control depending on what is injected. The only way I can see that this can be achieved is exposing the control or at least its interface.Any sugguestions?
ThinkAbout
+1  A: 

To answer your question:

You could likely create something that tracks this using a custom Region Behavior. Details of RegionBehaviors can be found here: http://msdn.microsoft.com/en-us/library/dd458944.aspx

You'd simply create a dictionary that tracked the region name and the associated control, since you have access to both from a Region Behavior.

If you need access to the control at the time a view is being added to the region, you would want to use a Region Adapter for this purpose (details at the same link above).

However, I agree 150% with Kent here that I smell you are doing something you should not. If you are trying to customize the layout or create animations / etc you should really focus on a strategy that doesn't require you to break the abstraction that Regions give you.

Something as simple as a style w/ data triggers could give you what you are looking for and wouldn't force you to know too much about the container from the code that adds the views to the region.

Anderson Imes
Hi, I agree with what you are saying, however, what if I have complex functionality such as modifying a property on the region control depending on what is injected. The only way I can see that this can be achieved is exposing the control or at least its interface. Any sugguestions?
ThinkAbout
You can use a Region Adapter to achieve this, but it sounds strange that you'd need to change the container based on the content without this being a Style, rather than something you do imperatively. I'd suggest exploring the possibility of using a Style here with DataTriggers.
Anderson Imes
A: 

ThinkAbout,

If you want to change a property on a module that is injected into a region, the best way to do it is to use either a routed Event or a routed Command depending on your situation.

The way I do it is, when the module is initialized I subscribe to events for for the properties that need to be changed. If any other module in the system need to change that property, I just fire the event.

I hope that puts you on the right track.

Regards Cornelius Kruger

Cornelius Kruger