Define Region(s) in Childwindows using Prism + MVVM

Topics: Prism v4 - Silverlight 4
Nov 29, 2011 at 3:30 PM

I am using Prism + MVVM to Inject views in regions. I use several Childwindows using Interaction requests, but I am unable to define Regions in these Childwindows. The RegionManager isn't avaibale/accesible in the xaml and xaml.cs files. With Interaction requests, I can't add parameters in the constructor, to pass the RegionManager.

From the View that shows the Childwindow, we call the following Interaction request from the ViewModel:

private void PopupCmdExecute( string str )
        {
            this._PopupRequest.Raise(
                new ChildWindows.PopupViewModel(_eventAggregator),
                dialog =>
                {
                    if (dialog.Result.HasValue && dialog.Result.Value)
                    {
                        
                    }
                });
        }

The interaction trigger in the XAML (of the View that raises the popup window):

    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding PopupRequest}">
            <prism:PopupChildWindowAction>
                <prism:PopupChildWindowAction.ChildWindow>
                    <vs:PopupView/>
                </prism:PopupChildWindowAction.ChildWindow>
            </prism:PopupChildWindowAction>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>

---

The following XAML code is from the PopupView that holds the Region:

<controls:ChildWindow x:Class="ApplicationModule.Views.ChildWindows.PopupView"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
           xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
           xmlns:prism="http://www.codeplex.com/prism"
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="780" Height="550" 
           Title="Popup"
           AutomationProperties.AutomationId="PopupView">

    <Grid>
       <ContentControl prism:RegionManager.RegionName="PopupTestRegion" 
                       VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" />
    </Grid>
</controls:ChildWindow>

--

Is it possible that I can still acces the RegionManager in the PopupView, without adding it to the constructor of the PopupView? (Because the interaction trigger 

<prism:PopupChildWindowAction>
doesn't allow parameters in the constructor...)
Developer
Nov 29, 2011 at 8:08 PM
Edited Nov 29, 2011 at 8:13 PM

Hi,

As a possible approach you could obtain the proper RegionManager in the constructor of your PopupView and set it as a dependency property of the view:

public PopupView()
{
    IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
    this.SetValue(RegionManager.RegionManagerProperty, regionManager);
    InitializeComponent();
}

In the previous code snippet, a new RegionManager is obtained through the Service Locator, however you might need to use an existing RegionManager or to create a new one and access it outside your PopupView. For doing this you could, for example, use a shared service (e.g. an object registered as a singleton in the container) to pass the RegionManager to/from the PopupView and obtain the shared service through the Service Locator. However, the specific details of how to implement this depends mostly of your personal preferences and the requirements of your scenario.

As a side note, if you use the view discovery approach (for example, registering a view in a region with the RegisterViewWithRegion method) it's not required for the view containing the region (in this case the PopupView) to have a RegionManager attached.

Also, in case you require a PopupChildWindowAction with a custom behavior (like for example, accepting parameters for the view) you might find the following thread useful where you can find an example of a custom PopupChildWindowAction:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini