Manage 2 regions containing 2 UserControls of same type

Topics: Prism v4 - WPF 4
May 30, 2013 at 7:43 PM
Edited May 30, 2013 at 7:46 PM

I am facing a design problem and would like your point of view.

I have the following shell :
<ItemsControl prism:RegionManager.RegionName="ParentRegion">
   <ContentControl prism:RegionManager.RegionName="SourceRegion" />
   <ContentControl prism:RegionManager.RegionName="DestinationRegion" />
An interface of the ViewModel contained in a "core" library shared across the libraries.
This ViewModel interface is the following :
public interface IMainRegionViewModel
        string SourceText { get; set; }
        string DestText { get; set; }
In my Module my initialize method set the 2 regions with the same type (MyView) :
public void Initialize()
      _regionManager.RegisterViewWithRegion("SourceRegion", typeof(MyView));
      _regionManager.RegisterViewWithRegion("DestinationRegion", typeof(MyView));
Consequently, during the creation of the MyView class, Unity injects a ViewModel of the same type in both my Views.

But I would like SourceRegion to update the field SourceText of the ParentRegion context and the DestRegion to update the field DestText of the ParentRegion.

What is the best way to do that?
  • Create 2 different view class inheriting from the same base view which only difference would be a function to override
  • Not using Unity for injecting the View in the regionManager and calling the View constructor with an argument of mine
    • I have some thoughts about implementing a delegate function as argument
  • Continue with the same code and share an event in the "Core" library that will use an EventArgs containing an information related with the View.
In fact I am looking for how to differentiate 2 Views injected by Unity in the regionManager so that they have a different behavior.

The goal of my question is : what is the best way to solve this problem?

May 31, 2013 at 6:07 PM

Based on my understanding of your application, a possible solution to your problem could be to react to the CollectionChanged event from the SourceRegion and DestinationRegion regions. Therefore, you will need to subscribe to this event in your ParentRegion view model and then change the SourceText or DestinationText accordingly. The following is an example on how to subscribe to them:
//SourceRegionViews_CollectionChangedHandler will modify SourceText
this.regionManager.Regions["SourceRegion"].Views.CollectionChanged += SourceRegionViews_CollectionChangedHandler;

//DestinationRegionViews_CollectionChangedHandler will modify DestinationText
this.regionManager.Regions["DestinationRegion"].Views.CollectionChanged += DestinationRegionViews_CollectionChangedHandler;
This way, your subscription handlers will be invoked every time a View is added in the corresponding region; there you will receive the injected View and you can execute the logic you are trying to achieve.

If this doesn't help or we misunderstood your problem, it would be helpful if you can provide us more details about your ParentRegion.


Federico Martinez