How can I close the popup window using the X button?

Topics: Prism v4 - WPF 4
Jun 14, 2012 at 1:22 PM
Edited Jun 14, 2012 at 1:23 PM

Hello
I would like to know. How can I close the popup window using the X button?

P.S. I am using RegionPopupBehaviors and I know about regionManager.Regions["PopupRegion"].Remove();

Developer
Jun 14, 2012 at 2:42 PM
Edited Jun 14, 2012 at 2:42 PM

Hi,

If you are implementing a popup region following the approach used in the NewsReader view in the StockTrader Reference Implementation, you will find that the DialogActivationBehavior class subscribes to the wrapper window Closed event, which will be fired when closing the window with the X button. If what you are looking is to remove the view from this region I believe, the subscribed handler for this event (ContentDialogClosed), could be the right place to do it. In the case of the Stocktrader RI you will find that the view is deactivated from the region:

 

private void ContentDialogClosed(object sender, System.EventArgs e)
        {
            this.Region.Deactivate(this.contentDialog.Content);
            this.CloseContentDialog();
        }

 

After, this the CloseContentDialog method is called, which will end up calling the Window.Close method of the wrapper window.

Please let me know if I have misunderstood your scenario,

Regards,

Agustin Adami
http://blogs.southworks.net/aadami

Jun 14, 2012 at 7:34 PM

Thank you very much, but I do not quite understand. How to remove the window? I wrote this code but it does not work.


        private void ContentDialogClosed(object sender, System.EventArgs e)
        {
            this.Region.Deactivate(this.contentDialog.Content);
            this.CloseContentDialog();
            var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            regionManager.Regions["PopupRegion"].Remove(sender);
        }

Developer
Jun 14, 2012 at 8:28 PM

Hi,

Take into account that when calling the region's Remove method you have to pass the object view that was added to the region's collection, and not the wrapper window like in your case (which is passed through the sender object).

For example, based on my understanding of your scenario your code could be like this:

private void ContentDialogClosed(object sender, System.EventArgs e)
        {
                     
        this.Region.Remove(this.contentDialog.Content);

        this.CloseContentDialog();
        }

In this case, I retrieved the view from the Content property of the wrapper window. Take into account, that you have to do this before calling the CloseContentDialog method, as it will end up setting the Content property to null.

Also, note that I'm using the Region property of the RegionBehavior to remove the view, as it's not required to retrieve the RegionManager instance from the container.

I hope you find this handy,

Agustin Adami
http://blogs.southworks.net/aadami