Help with multiple views issue

Topics: Prism v2 - Silverlight 2
Jul 14, 2009 at 5:19 PM


I  have two views A and B which belong to the same module.  View B is a user control which I use in view A.

Each view has its own ViewModel.

I register view A like this :

_regionManager.RegisterViewWithRegion("SourceListRegion", () => _container.Resolve<IViewA>().View);

_regionManager.RegisterViewWithRegion("SourceListRegion", () => _container.Resolve<IViewB>().View);

and then :

_container.RegisterType<IViewA, ViewA>();
_container.RegisterType<IViewAViewModel, ViewAViewModel>();
_container.RegisterType<IViewB, ViewB>();
_container.RegisterType<IViewBViewModel, ViewBViewModel>();

Ok, as you can see I resolve view B when registering with the same region as view A. Is this the correct way to do it ? I find it strange to have to register a view with a region when this view is actually contained in a view (view A) already registered with that region.

Second issue, which might more a MVVM problem : In the constructors of my views, I register the model with the view like that :

public ViewAViewModel(IEventAggregator aggregator,
                                 IViewA view)
      View = view;


public ViewBViewModel(IEventAggregator aggregator,
                                 IViewB view)
      View = view;



For a reason I can't explain, view B ends up having its ViewModel set to the one of view A..... how come ?


thanks !





Jul 16, 2009 at 1:22 PM


About View Registration
As you say the code above is registering both views in the same region, which is not what you want if ViewB should be inside ViewA. The approach in this situation is creating a region inside ViewA and register/inject ViewB into that region. For some situations creating a scoped region is also useful, so perhaps you might take it into account.

About Having the Same Model in both Views
From the code above I cannot find the reason why this would be happening. You can debug your solution to check why both views are getting the same model and take a look at the Reference Implementation solution that comes with the guidance, which uses PresenationModels/MVVM and check if your code has any differences with it. If you cannot find the cause and the error persist, please provide some more code so we can reproduce the error and find a possible fix.

Side Note: The code above is resolving the Views and trying to get a View property:
_regionManager.RegisterViewWithRegion("SourceListRegion", () => _container.Resolve<IViewA>().View); 

The common approach is having a View property in the model:
_regionManager.RegisterViewWithRegion("SourceListRegion", () => _container.Resolve<IViewAViewModel>().View);

This most likely is an error when you posted your question, but in case it is not it could have something to do with having the same ViewModel in both views.

I hope my answer was useful.

Damian Schenkelman

Jul 16, 2009 at 1:40 PM

Hi Damian,

I had a look at scoped region but I did not realize it was what I needed to use.... You are also saying that I can create a region inside ViewA, how can I do that ? Do you have a code example ?


You are right, it was a typo when I posted my code. I do have a View property in my ViewModel.

I forgot to post the code of the SetViewModel function :

public void SetViewModel(ISourceFilterViewModel viewModel)
      DataContext = viewModel;

As you can see I set the DataContext of the view to the viewModel passed as parameter (see first post). I believe that when I set the DataContext for ViewA, it also sets it for ViewB... But I don't know how to prevent this from happening.

Jul 17, 2009 at 3:01 PM


To create a region inside a view you simply need to add the code you would to create a region in the Shell to a view. For example:

<UserControl xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation" ...>
      <Grid x:Name="LayoutRoot" Background="{x:Null}">
            <ItemsControl x:Name="OrdersItemsControl" BorderThickness="0" cal:RegionManager.RegionName="OrdersRegion" Margin="0,10,0,10" />

Please let me know if this helps.

Damian Schenkelman

Sep 28, 2010 at 1:30 AM

Are you sure you can do this?

I have a problem at the moment when i add a region inside another region (similar to the way you just described it, using RegionManager.RegionName="...") and it causes RegionCreationExceptions and/or doesn't dispose the views.

I thought with View Discovery composition it can only be one region deep, is that correct?

Sep 28, 2010 at 8:10 PM


There is no known restriction regarding nested regions in the View Discovery approach. Therefore, the exception you are experiencing might be caused by the following reasons:

  • The application creates two regions with the same name: unless you use scoped regions, you can’t set two regions with the same name, even if they are nested within each other. That’s because a RegionManager cannot hold two regions with the same name. In case you use scoped regions, you would be using a separate RegionManager for each region, so it wouldn’t throw an exception.
  • You could be setting the region in an inappropriate control: out-of-the-box, regions can only be defined in a control that derives from ItemControl, ContentControl or Selector. If you try to create a region in a control that does not derive from any of the mentioned, an exception will be thrown.

I hope this helps.

Fernando Antivero