Behavior without region adapter?

Topics: Prism v4 - WPF 4
Nov 12, 2010 at 12:57 PM
Edited Nov 12, 2010 at 12:58 PM


I want to create a behavior which would call Load/Unload methods for ViewModels connected with Views.

I understand that I can create a RegionAdapter and override AttachBehaviors method. But my region is ContentControl for which an adapter already exists.

How can I create a behavior without creating a RegionAdapter?

Nov 12, 2010 at 1:51 PM


If your scenario requires to create a Region Behavior, it will be not necessary to modify an existent Region Apdater or create a new one. But first of all, I would suggest you to take a look at the RegionMemberLifetimeBehavior, which is part of Prism 4. This behavior removes views from a region when they become deactivated. So you could use this as a reference as well as you could extend this behavior to call methods in your view models. For more information about this you could check the RegionMemberLifetimeBehavior Class on MSDN.

If you decide to create a new Region Behavior, you need to add this for all regions by overriding the ConfigureDefaultRegionBehaviors in the bootstrapper. So you code might be similar to the following:

protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
IRegionBehaviorFactory factory = base.ConfigureDefaultRegionBehaviors();
factory.AddIfMissing("MyBehavior", typeof(MyCustomBehavior));

If you need more guidance on how to create your own Region Behaviors, you could check the following section on MSDN: UI Composition, see the Extending Region Behaviors section.

Hope this helps.

Fernando Antivero

Nov 12, 2010 at 6:04 PM


RegionMemberLifetimeBehavior looks nice, I've implemented my behavior with the same idea - monitoring Region.ActiveViews collection.

Adding a behavior of all regions (in the bootstrappers's ConfigureDfaultRegionBehavior) can be handy, but not in my case. I need to add my behavior for the only one region (which is ContentControl). How to do this?

Nov 12, 2010 at 6:30 PM


This is not possible in Prism out-of-the-box. But there are different ways to achieve this scenario. For example, if you use the RegionActiveAwareBehavior, it is required to implement the interface IActiveAware in your views. Following this example, you could implement an interface either in your views or view models in which you want to call to the Load/Unload method. This way, in your custom behavior, you could evaluate if an object implement a specific interface. So your code might be similar to the following:

var activeAware = item as IActiveAware;

if (activeAware != null)

For more information you could take a look at the RegionActiveAwareBehavior class.

On the other hand, you could create a Attached Property for your regions and add this in those regions of your choice. But I would suggest you to follow the approach proposed by the product team, which is implementing a interface in particular classes. If you have a lot of classes, you might prefer to create a base class for this purpose and implement on it the interface.

Hope this helps.

Fernando Antivero


Nov 12, 2010 at 6:37 PM

I've found the way.

In my bootstrapper's InitializeShell:

protected override void InitializeShell()
IRegion region = Container.GetExportedValue<IRegionManager>().Regions[RegionNames.MainRegion];
region.Behaviors.Add(ViewModelRegionBehavior.BehaviorKeynew ViewModelRegionBehavior());



Nov 12, 2010 at 6:44 PM


Sorry, I missed that detail, nice to see that you found the solution for your scenario. And thanks for sharing this with the rest of the community, since other users with a similar situation can find this really helpful.

Fernando Antivero