Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF, Prism v2, Region in a modal dialog, add region in code behind

I have a composite WPF application. In one of my modules I want to make a wizard and have the steps show up in a region so I can switch between the steps easier. Originally I had this wizard showing up in a tab region and the nested region worked fine. Now I want to make it into a modal dialog box, but after I open it the inner region never gets registared with the region manager; So I can't add my wizard steps.

I was under the impression that the region manager was global, and just adding cal:RegionManager.RegionName="WizardSteps" would do it, but apparently not.

If i pass the region manager to the view I might be able to use it...Does anyone know how to add a region to a ContentControl in code behind?

like image 354
Shaboboo Avatar asked Jun 18 '09 20:06

Shaboboo


2 Answers

It is actually quite simple.

In your popup xaml add a regionname as you do in the shell. Then in the popups constructor, add the following call:

public Popup(IRegionManager regionManager)
{
     InitializeComponent();
     RegionManager.SetRegionManager(this,regionManager);
}

This works for me in Prism v.1 - shouldn't be too much different in later versions.

like image 167
Goblin Avatar answered Mar 31 '23 16:03

Goblin


The problem is that regions search up the visual tree for the RegionManager attached property, and then register themselves with that manager. In the main window that's fine, but in a child window this doesn't happen.

In the Bootstrapper, after the shell is created, the following code is performed.

RegionManager.SetRegionManager(shell, this.Container.Resolve<IRegionManager>());
RegionManager.UpdateRegions();

To get the region manager to work with your child window do the same thing right after you've created the window.

EDIT

To set the region name of a control, you also set the attached property of the RegionManager, like so...

RegionManager.SetRegionName(control, "MyRegion");

However you can do this in xaml aswell. The reason why your regions in a separate window don't work is because the RegionManager needs to be set on the base window, like I showed above.

like image 27
Cameron MacFarland Avatar answered Mar 31 '23 16:03

Cameron MacFarland