Closing popup without using X button?

Topics: Prism v4 - WPF 4
May 25, 2011 at 4:48 PM

I wanted to add a file menu to the news reader popup so that the user could close the window a different way.

What code do I need to put in my view model so that the DialogActivationBehavior will fire just like when the X button is pressed?

Developer
May 26, 2011 at 3:00 PM
Edited May 26, 2011 at 4:21 PM

Hi,

You could try removing the view from the popup region you have created. That would cause the popup to be closed.

For example, if you are using navigation, and you have registered your view in the DI container you're using with a contract named "SomeView", your code would look like this:

 

this.regionManager.Regions[RegionNames.PopupRegion].Remove(ServiceLocator.Current.GetInstance<object>("SomeView"));

 

Or if you have your view registered in the container with its type as the contract, your code could look like this:

 

this.regionManager.Regions[RegionNames.PopupRegion].Remove(ServiceLocator.Current.GetInstance<SomeViewType>());

 

Assuming this.regionManager is an instance of the region manager, and the view is registered as a singleton instance.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

May 26, 2011 at 6:01 PM

Thanks!

For some reason I was having issues with the ServiceLocator not finding the view so I made a method in my controller that can remove the views.

public void RemoveView(string regionName, string viewName)
{
    // Check to see if the region exists.
    if (this.regionManager.Regions.ContainsRegionWithName(regionName))
    {
        IRegion region = this.regionManager.Regions[regionName];

        object currentView = region.GetView(viewName);

        // If the view exists in the region remove it.
        if (currentView != null)
        {
            region.Remove(currentView);
        }
    }
}