MEF & WPF Region not found

Topics: Prism v4 - WPF 4
Jun 21, 2011 at 8:40 PM

Hi there,

I'm creating a new WPF with MEF and it works nice.

But now I need to create a new Window with a Region but it seems like the region is not registered. However the Regions created in the Window (shell) are registered.

Here's the code for the new Window


<DockPanel LastChildFill="True">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" DockPanel.Dock="Bottom">
<Button Content="_Previous" Margin="0, 0, 10, 0" />
<Button Content="_Next" />
<ContentControl cal:RegionManager.RegionName="WizardStepsRegion" />

When I try to get the view inside the region I get the following exception "KeyNotFoundException was unhandled by user code", "The region manager does not contain the WizardStepsRegion region."


private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
object asdf = regionManager.Regions["WizardStepsRegion"];//.GetView("Step1View");

Can someone help me to fix this?

Thank you.

Jun 22, 2011 at 2:22 PM


Based on my understanding of your scenario, your problem might be happening because your window doesn’t have the RegionManager property set. You could try doing the following:


IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
regionManager.SetRegionManager(this.MyWizardWindow, regionManager);

If that doesn’t make your region appear in the region manager, you could try to registering the specific region manually into the RegionManager. For example like this:

IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
regionManager.SetRegionName(this.MyContentControl, "WizardStepsRegion");


Additionally, you could try using the View Discovery approach (that is to say, using the RegisterViewWithRegion method to register views into regions), so that you make sure that your views are added to the regions when they are actually created.

You can find more information about Regions in Prism in Chapter 7: Composing the User Interface.

Please let me know if this information helps you.



Miguel Bronzovic

Jun 23, 2011 at 1:36 PM
Edited Jun 23, 2011 at 1:37 PM

Hi mbronzovic,

I followed the first approach and it works :-)

This is the updated constructor of my new Window

public Wizard(IRegionManager rm, CompositionContainer cc)

	if (firstTimeRunning)
		firstTimeRunning = false;

		RegionManager.SetRegionManager(this, rm);
		rm.RegisterViewWithRegion("WizardStepsRegion", typeof(Step1View));


And this is the way I show it


Wizard wizard = (Wizard)ServiceLocator.Current.GetInstance(typeof(Wizard));


Can you please tell me why do I've to add the SetRegionManager(...) ?

If I omit it how can I access to the region? Where is it registered? Because the view is shown without the SetRegionManager().

Thank you


Jul 12, 2011 at 7:23 PM

I have also had this exact same problem and would be very keen to see the answer to your last question in this thread.

Many thanks in advance.


Jul 14, 2011 at 7:21 PM

Hi Simon,

The RegionManager property is an AttachedProperty, which the RegionManagerRegistrationBehavior attempts to locate in parent controls browsing through the visual tree. However, for controls that aren't actually children of a control that has the region manager defined (i.e. a popup window), you must manually set the RegionManager AttachedProperty, as it was done in the example calling the RegionManager.SetRegionManager method.

I hope you find this helpful.

Guido Leandro Maliandi

Jul 19, 2011 at 3:05 PM
Edited Jul 19, 2011 at 8:37 PM

Hi there,

The code that I posted on jun 23 in this thread works well the first time I run the wizard however the second time I run it I get the exception RegionCreationException in the statement wizard.ShowDialog();

Here's the detailed info: An exception occurred while creating a region with name 'WizardStepsRegion'. The exception was: System.ArgumentException: Region with the given name is already registered: WizardStepsRegion.

How can I fix this?

Thank you.

Jul 21, 2011 at 4:44 PM
Edited Jul 21, 2011 at 4:59 PM


You could try decorating your Wizard class with the PartCreationPolicy attribute with the CreationPolicy.NonShared value, so that a new instance of your wizard is created each time you resolve it through the service locator.

Edit: I've checked your new thread about this issue; I'll examine your repro sample and provide an answer about it in that thread.

I hope you find this helpful.

Guido Leandro Maliandi

Jul 21, 2011 at 5:10 PM

Hi Guido,

I've posted a new post with a simple application at where I give a better explanation about it.

The Wizard class is decorated with PartCreationPolicy as you said so that's not the problem.

Thank you for your help.

Jul 21, 2011 at 5:59 PM


I've already provided a response in your new thread, so let's continue our discussion in that thread.


Guido Leandro Maliandi