Adding region behaviors in the shell.

Topics: Prism v2 - WPF 3.5, Prism v2 - WPF 4
May 27, 2010 at 10:50 PM
Edited May 27, 2010 at 11:02 PM
Greetings -

I've got some sliders in my application (basically popups). When the user first runs the application they are open. I want to close them when they move to another area the app (in this case activate a view on another region). I'm not sure how to go about doing this... my guess is adding a RegonBehavior to the MainRegion that fires an event when a new view is activated....-

Here is an example of what I'm talking about.

    <ContentControl cal:RegionManager.RegionName="{x:Static regions:RegionNames.MainRegion}"/>
    <cc:Slider IsExpanded="True" cal:RegionManager.RegionName="{x:Static regions:RegionNames.MessageCenter}"/>
    <cc:Slider IsExpanded="True" cal:RegionManager.RegionName="{x:Static regions:RegionNames.AppointmentCenter}"/>
    <cc:Slider IsExpanded="True" cal:RegionManager.RegionName="{x:Static regions:RegionNames.WeatherCenter}"/>

Basically the three sliders need to know when the content in the content control is changed.

What would you do??

EDIT: Forgot to mention the related question - If I do add a behavior to the main region - how? I can't add it in the code behind of the shell because the region hasn't been added to the region manager yet, and I can't add it in the bootstrapper for the same reason...??
Oct 1, 2010 at 9:28 PM
Edited Oct 1, 2010 at 9:41 PM


I think that you can find interesting the following forum thread where it is used the RegionActiveAwareBehavior: Detecting view model close

Regarding to how to implement your own region behavior, it is necessary to implement IRegionBehavior or derive from RegionBehavior class. Then you should register this by overriding the ConfigureDefaultRegionBehaviors method in your bootstrapper class (pseudo-code):

protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
    var factory = base.ConfigureDefaultRegionBehaviors();
    factory.AddIfMissing(“MyBehavior”, typeof(YourRegionBehavior));

Additionally, as you want to add this in your MainRegion, your code can be similar to the following (pseudo-code):

var mainRegion = regionManager.Region[“MainRegion”];
mainRegion.Behaviors.Add(“YourBehavior”, new YourRegionBehavior());

If you need more information on this topic, you could take a look a the following the documentation section on MSDN: UI Composition

I hope this helps.

Fernando Antivero

Oct 1, 2010 at 9:38 PM

If you override ConfigureDefaultRegionBehaviors, you will probably want to call the base implementation at the end of your implementation to ensure any core items you do not register get properly hooked up.

That RegisterIfMissing method shoudl ensure that there are no double registrations and allow you to "override" the built in implementations by registering your implementations first.