Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF Main and Child Window Management

0.00/5 (No votes)
8 Feb 2012 5  
WPF Main and Child Window Management

Introduction

In our WPF application, we call any child window in our event, examples are button click event or other click event. When we call a child window from our main window, normally it loads our main window. But if we click on that event again, it will load again in the main window.

It’s really a big problem for our WPF application. Now we will manage our child window in this scenario that if the child window loads once, it will not load newly again on its event. If we minimize it, then it will appear in the main window bottom as Taskbar. If we click on its event, it will load from minimize. And only the main window will load on our computer Taskbar. There will no child window loaded on Taskbar.

I have create a method named showWindow. Let's see how the method works.

This foreach loop finds our all current UIs from namespace using split if it finds out the desired window, it just loads it on the main window.

foreach (Window objWindow in Application.Current.Windows)
                {
                    string[] splitedNamespace = (objWindow.ToString()).Split('.');
                    string aClassNameFromCollection = 
            splitedNamespace[splitedNamespace.Length - 1];

                    if (aClassNameFromCollection == className)
                    {
                        isOpen = true;
                        objWindowName = objWindow;
                        break;
                    }
                }

If the window is already open, it will set the window state to normal.

if (isOpen)
                {
                    foreach (Window objWindow in Application.Current.Windows)
                    {
                        string[] splitedNamespace = (objWindow.ToString()).Split('.');
                        string aClassNameFromCollection = 
            splitedNamespace[splitedNamespace.Length - 1];

                        if (aClassNameFromCollection == className)
                        {
                            objWindowName.WindowState = WindowState.Normal;
                            objWindowName.Activate();
                            break;
                        }
                    }
                }

If the window is not open, it will load your desired window. When the case will match with your window name, it will load and break the statement.

if (isOpen == false)
                {
                    #region SHOW DESIRED WINDOW
                    switch (className)
                    {
                        case "EmployeeSetupUI":
                            EmployeeSetupUI employeeInfo = new EmployeeSetupUI();
                            employeeInfo.Owner = this;
                            employeeInfo.Show();
                            break;
                        case "CalenderSetupUI":
                            CalenderSetupUI calendarSetup = new CalenderSetupUI();
                            calendarSetup.Owner = this;
                            calendarSetup.Show();
                            break;
                    }
                    #endregion SHOW DESIRED WINDOW
                }

Now consider the window state: Normally if we minimize the main window, the child window can’t minimize and similarly maximize. We will manage this scenario on the main window state change event.

For minimize, the main window finds out all open child window states using for loop and for maximize, it sets all child window states with foreach.

if (WindowState.Minimized == this.WindowState)
                {
                    int numberOfChildWindow = this.OwnedWindows.Count;
                    childWindows = new Window[numberOfChildWindow];
                    for (int count = 0; count < this.OwnedWindows.Count; count++)
                    {
                        childWindows[count] = this.OwnedWindows[count];
                    }
                }

                else if ((WindowState.Maximized == WindowState) || 
            (System.Windows.WindowState.Normal == WindowState))
                {
                    if (childWindows != null)
                    {
                        foreach (Window aChildWindow in childWindows)
                        {
                            aChildWindow.WindowState = WindowState.Normal;
                            aChildWindow.Show();
                        }
                    }
                }

Note: For all child windows, we have to set ShowInTaskbar property as false because it will show in our main window Taskbar. And on every child window closing event, we have set this.ShowInTaskbar = true and this.Owner = null to break down all Owners with our main window.

History

  • 7th February, 2012: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here