简体   繁体   中英

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?

I don't know that framework but I suspect that an MDI child is not a Window . When you call Window.GetWindow(this).Close(); you close the main window and therefore terminate the application.

Try to remove the MdiChild from the MainMdiContainer :

var child = new MdiChild() { ... };
MainMdiContainer.Children.Add(child);
// Later
MainMdiContainer.Children.Remove(child);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM