WPF Close all child window except active child window and main window

c#, wpf

Solution

As far as I know, MDI support for WPF is limited, so try using the Tag attribute when you create your pseudo-child windows:

Window child = new Window();
child.Tag = "mdi_child";

Then, in your loop, modify it like this:

foreach (Window win in App.Current.Windows)
{
    if (!win.IsFocused && win.Tag.ToString() == "mdi_child")
    {
        win.Close();
    }
}

Note that for the above solution to work, ALL windows must have a Tag attribute, or else an Exception will be thrown at `win.Tag.ToString()`.

Problem

I have C# WinForms code as follows to close all child forms when I open a new child form: ``` private void CloseAllActiveForms(Form[] MdiChildren) { Form[] childArray = MdiChildren; foreach (Form childform in childArray) { childform.Close(); } } ``` How can I use in WPF windows? I tried the below code, but it will close all windows including the Parent and the Active window. ``` private void CloseAllWindows() { for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--) { Application.Current.Windows[intCounter].Close(); } } ``` Thanks.

Original source