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

↘锁芯ラ 提交于 2019-11-28 09:14:55

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.

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.

Shaboboo

I found something thats almost working. I'm sure if i could bind the region's active view to the contentContol's content property then it would work, but I haven't managed that yet.

IRegionManager MyRegionManager = container.Resolve<IRegionManager>();
SingleActiveRegion newRegion = new SingleActiveRegion();
MyRegionManager.Regions.Add("WizardSteps", newRegion);

//Binding
Binding myBinding = new Binding("ActiveViews");
myBinding.Source = newRegion;
view.stepControl.SetBinding(ContentControl.ContentProperty, myBinding);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!