How to close a Popup from the viewmodel

Topics: Prism v2 - WPF 3.5
Aug 15, 2009 at 7:02 AM


I use the popup approach to show my modals windows and everything works well. Now i would like to close the windows when the user click on a  button in the popup.

This should be done in the viewmodel as i use MVVM pattern. Before Prism i used a mediator and an attached property but this approach fails

in Prism as the the view is now a UserControl and not a Windows. When i try to access the parent of the view i get null in the mediator.

Any help will be appreciate.


Aug 18, 2009 at 6:02 PM



I can think of two possible ways to get the owner window (popup) from the ViewModel. One is using a recursive call, which is useful for any way you are placing the view in the region:
private Window owner       
public Window Owner
                if (owner == null)
                    owner = GetOwnerWindow(this.View as DependencyObject);
                return owner;

private static Window GetOwnerWindow(DependencyObject container)
            Window window;
            var parent = LogicalTreeHelper.GetParent(container);
            window = parent as Window;
            if (window != null)
                return window;
            return GetOwnerWindow(parent);

If you are using the RegionPopupBehavior from the RI you can simply ask for the view’s parent, which should be the Window.

As you are using the MVVM pattern, you might implement this using Commands and have the view directly bind to it.

Please let me know if this helps.

Damian Schenkelman

Aug 18, 2009 at 6:36 PM

Thank Damian

I do pure MVVM so the viewmodel know nothing about the view. I set the DataContext of the view in its constructor or in Xaml.

This means no way to have access to the view from the viewmodel so i can not request its parent nor do a recursive call.

I don't know if using pure MVVM is a good practice. May be i'll change

But as i use RegionPopupBehavior from RI i realize that i could simply remove the view from the region and the window closes.

Thank you very much.

Francois Blochaou


Aug 18, 2009 at 7:12 PM

Hi Blochaou,

I blog on the topic on the following link (note Presentation Model and MvVM are synomonous):

Aug 18, 2009 at 7:49 PM

Hi Billkrat

It is a nice solution.


Jan 10, 2010 at 6:59 PM

My ViewModel raises an event that the View subscribes to.  The event handler in the View closes itself.


Jul 27, 2010 at 11:17 PM


I have implemented the RegionPopupBehavior as in the RI of Prism. I want to bring up a new window pop up on a button click. I am activating the view in a Secondary region as RI. How to command bind a button command to open this new window vie

Oct 4, 2010 at 3:18 PM


You can find interesting the following documentation section on MSDN: Commands (see the Binding Commands to a View section).

Hope this helps.

Fernando Antivero