Region.add and RegisterViewWithRegion

Topics: Prism v2 - WPF 3.5
Jun 30, 2009 at 6:13 PM
Edited Jun 30, 2009 at 6:17 PM

Hallo,

I read this article http://blogs.southworks.net/jdominguez/2008/09/presentation-model-with-datatemplates-in-compositewpf-prism-sample/ and aplyed Julian's suggestion to my new Project. 

Now i am trying to use RegisterViewWithRegion. It seems that the Regions I created in the C#-Code, aren't connected to the Register of the Views.

I hope the Code will make it clear.

Module 1 Infront of Module 2

_regionManager.RegisterViewWithRegion("SubMenuRegion", () => _container.Resolve<ISubMenuEntryViewModel>());

Module 2 After Module 1

SubMenuRegion = new Region()
regionManager.Regions.Add("SubMenuRegion", SubMenuRegion);

Is this a bug(missing feature) of me or Prism v2? Is there a work around?

Thanks & Gruß Timo

Jul 1, 2009 at 5:26 PM

Hi GiCo,

 

View Discovery was introduced in Prism-v2 and the post you mention from Julian Dominguez targets Prism-v1. On the other hand, when you create regions by code region behaviors are not attached. As Julian says in his post:

 "One drawback of this particular implementation is that if I have a more complex Control that not only binds, but interacts with a region, there is no out-of-the-box behavior that will use Region Adapters to enhance this binding (in the example the ItemsControl just binds to the Views collection exposed by the region and that’s it)."

 

To workaround this issue you might register the AutoPopulateRegionBehavior (which is in charge of listening to the IRegionViewRegistry changes and populate the region content) for the regions you create by code (this is automatically done by region adapters when regions are registered in XAML). The code should be similar to the following:

            SubMenuRegion = new Region();

            MainRegion.Behaviors.Add(AutoPopulateRegionBehavior.BehaviorKey, unityContainer.Resolve<AutoPopulateRegionBehavior>());

            regionManager.Regions.Add("SubMenuRegion", MainRegion);

 

Depending on your scenario, you might need to register other behaviors. You can find prism behaviors  in the Region/Behaviors folder of the Composite.Presentation.Desktop project. You can also you the IRegionBehaviorFactory to get the default behaviors

 

Hope it helps!

 

Matias Bonaventura

http://blogs.southworks.net/matiasb

Jul 2, 2009 at 12:12 PM

Thank you a lot! Now everything works fine.

I like prism a lot, great work. Articles like the one of Julian Dominguez helped me also a lot.

Gruß Timo