Search code examples
c#wpfpopupprismregion

PRISM Region Disappears when Popup is Created


I have a a PopUp with a Region that contains another Region. This popup is invoked through the WPF Prism(MEF) InteractionRequest methodology. The structure looks like so:

PopUpUserControl
  - ContentControl : Region(UserCatalogsCreateRegion)
     - PopUpStageUserControl
       - StackPanel 
          -ContentControl : Region(UserCatalogsCreateStackRegion) <--Disappearing Region

The problem manifests itself like this. When the application starts up and is running normally, I can list the Regions in the application and I can see that the RegionManager contains the Region named "UserCatalogsCreateStackRegion".

Now when I click the button that sets off the InteractionRequest for PopUpCreation, I can see that the list of Regions no longer contains "UserCatalogsCreateStackRegion". I verified that something is removing my Region because I added a CollectionListener to the Regions property of the RegionManager, and as soon as the Popup is created, my breakpoint is hit and the Notif..Action is "Remove" and the OldItem is the Region in question.

TL;DR Region disappears from RegionManager.Regions when the popup that contains said Region is created and invoked.

Any help is greatly appreciated. And I will try to answer as many other questions as possible as there is A LOT that can go wrong with a Region manager.

EDIT

Brian Lagunas' links pointed right to the doggone solution. This was the solution. My final working code for the PopUpStageControl looks like this, where ContentControl is the Region that kept "disappearing":

    [ImportingConstructor]
    public PopUpStageUserControl(IRegionManager regionManager)
    {
        InitializeComponent();
        this.regionManager = regionManager;

        //Fix Begin
        RegionManager.SetRegionName(ContentControl, AppRegions.UserCatalogsCreateStackRegion);
        RegionManager.SetRegionManager(ContentControl, regionManager);
        //Fix End

        RegionManager.SetRegionManager(this, regionManager);
        RegionManager.UpdateRegions();
    }

Solution

  • This is because a popup is not part of the visual tree, so the region manager will not be able to find the region. You will have to manually register the region. See these posts:

    Region not loaded by the RegionManger

    How to register regions inside user controls or control templates in RegionManager?

    PRISM 6 Regions in WPF using DataTemplate/CustomControl

    https://github.com/PrismLibrary/Prism/issues/251