Closing MDI Child window from within content control

Mar 14, 2011 at 11:43 PM


I have a requirement to disable the close button on an MDI window, or to be specific save data when the user exists a window.

Is there a way I can close the MDI child control from within the content controls code behind?

this.Close(); for example

Thank you

Mar 15, 2011 at 1:31 AM

Apologies this requirement has been resolved. Passed reference to the the control of the mdi child.

Oct 9, 2011 at 4:11 PM

what do u mean by "Passed reference to the the control of the mdi child"? i have the same problem

the code doesn't work because Me is a UserControl while Remove([mdiChild])

this is the code


Dim parent as WindowMain = Window.getWindow(Me)



Oct 9, 2011 at 10:25 PM

You should be able to execute the close command of the mdiChild

MdiChild child = new MdiChild();



That removes the MdiChild from the observerable collection etc

I have a ServiceLocator class that basically just loads windows (attaches mdiChild to the container), I pass the child to my View Model via the constructor.

Oct 10, 2011 at 5:07 AM

this is the code to attach mdiChild in the main window

Private Sub menuNewOrder_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim mdiChild As New MdiChild With
            .Title = "New Order",
            .Content = New FormNewOrder()

    End Sub

this is the child's code

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
'what am i suppose to type here?
    End Sub

Oct 10, 2011 at 6:03 AM

In your main window you could do this (this is C#) Have a static reference to your main window or even the container in the main window

public static yournamespace.yourmainwindow mainwindow;

then in your constructor of the main window do this:

mainwindow= this;   

For example: I have this code

 public partial class Shell : Window   
public static CareSoftware.Shell mw;                             

        public Shell()       
mw = this;           


then your can access the container from anywhere is your project like the following:



if you wish to access the mdiChild in your window usercontrol, you will need to pass a reference to it when you create the window

 MdiChild child = new MdiChild();      
//enter child properties

 MyWindow win = new MyWindow(child);  // do this here, MyWindow is the UserControl, MyWindow is the class name of that user control
yournamespace.yourmainwindow.mainwindow.Container.Children.Add(child); // add the child to the container so it appears in the shell

And then in your window (MyWindow)

MdiChild view; //declare this

public MyWindow(MdiChild _view)

view = _view; // assign 


In your cancel click event:
view.Close(); // remove from the container. The underlying MDI project takes care of the rest.

Oct 13, 2011 at 10:27 PM

This may be a better implementation. You will need to have a public Button CloseButton in the MdiChild class thou. I have struggled to be able to get reference to the button in the mdiChild template. If anyone can then please provide. I have tried a fair few different ways.

You need to call the ApplyTemplate, otherwise the close button is null

This will go in the constructor of your window where _view is the mdiChild:



this.Dispatcher.BeginInvoke((Action)(() =>           


_view.CloseButton.Command = vm.ExitCommand;            }),