Search code examples
c#wpfwindowsmdiparent

How to close only child on click of a button in WPF


I have created MDI form using CodePlex DLL, I have added a WPF user control and opened it as a child with below code:

MainMdiContainer.Children.Add(new MdiChild()
        {
            Title = " Employee Master",
            Height = 300,
            Width = 500,
            //Here CustomerMaster is the class that you have created for mainWindow.xaml user control.
            Content = new EmpMaster()
        });

Now I want to close only that child form (WPF User Control) on Click of a button.I have used following code on that button click:

Window.GetWindow(this).Close();

But that code close the program along with all opened window and MDI parent also. How can I close only that particular WPF user control on button click?


Solution

  • I got the answer, I have called below lines of code on that button click:

    MainWindow mainWind = Application.Current.MainWindow as MainWindow;
    mainWind.MainMdiContainer.Children.RemoveAt(0);