Managing Regions Across Multiple Windows in Prism.WPF

When working with Prism.WPF and dependency injection containers like Unity, developers commonly register the primary application shell within the CreateShell method override. This process establishes the initial IRegionManager instance associated with the main window.

protected override Window CreateShell()
{
    return Container.Resolve<MainWindow>();
}

However, complications often arise when spawning secondary windows and attempting to utilize scoped regions within them. Consider a scenario where a secondary window is instantiated and displayed via the container:

var secondaryWindow = Container.Resolve<SecondaryWindow>();
secondaryWindow.Show();

The XAML definition for this secondary window might include a specific region for content injection:

<Window x:Class="App.Views.SecondaryWindow">
    <Grid Background="DarkGray">
        <ContentControl prism:RegionManager.RegionName="SubContentRegion" />
    </Grid>
</Window>

Inside the code-behind or view model of this secondary window, developers frequently attempt to access this region immediately upon loading to inject views:

public class SecondaryWindow : Window
{
    private readonly IRegionManager _regionManager;
    private readonly UserControlA _viewA;
    private readonly UserControlB _viewB;

    public SecondaryWindow(IContainerExtension container, IRegionManager regionManager)
    {
        InitializeComponent();
        
        _regionManager = regionManager;
        _viewA = container.Resolve<UserControlA>();
        _viewB = container.Resolve<UserControlB>();
        
        this.Loaded += OnWindowLoaded;
    }

    private void OnWindowLoaded(object sender, RoutedEventArgs e)
    {
        var targetRegion = _regionManager.Regions["SubContentRegion"];
        targetRegion?.Add(_viewA);
        targetRegion?.Add(_viewB);
        targetRegion?.Activate(_viewA);
    }
}

Executing this code typically triggers a System.Collections.Generic.KeyNotFoundException at the line where the region is retrieved. The exception indicates that the SubContentRegion does not exist within the current IRegionManager instance.

Resolution Strategy

The root cause of this issue is scope mismatch. The IRegionManager injected via the constructor is the global instance associated with the main shell (created during CreateShell). It is not automatically aware of regions defined in dynamically created secondary windows. To resolve this, a new IRegionManager must be created specifically for the secondary window and attached to it.

The recommended approach is to resolve a new region manager from the container within the secondary window's constructor and explicitly associate it with the window using the RegionManager.SetRegionManager attached property. Afterward, call RegionManager.UpdateRegions() to ensure the window's visual tree is scanned and the new regions are registered.

public SecondaryWindow(IContainerExtension container)
{
    InitializeComponent();

    // Resolve a new RegionManager instance for this specific window scope
    _regionManager = container.Resolve<IRegionManager>();
    
    _viewA = container.Resolve<UserControlA>();
    _viewB = container.Resolve<UserControlB>();

    // Bind the new manager to this window instance
    RegionManager.SetRegionManager(this, _regionManager);
    
    // Re-scan the visual tree to register regions with the new manager
    RegionManager.UpdateRegions();
    
    this.Loaded += OnWindowLoaded;
}

This configuration ensures that the SubContentRegion is registered within the newly scoped _regionManager, allowing subsequent navigation and view management operations to succeed without exception.

Tags: WPF Prism C# Dependency Injection UI Architecture

Posted on Fri, 21 Aug 2026 16:28:07 +0000 by aleczapka