Click here to Skip to main content
15,881,455 members
Articles / Desktop Programming / WPF
Article

WPF Main and Child Window Management

Rate me:
Please Sign up or sign in to vote.
3.67/5 (5 votes)
8 Feb 2012CPOL2 min read 62.6K   2.2K   10   9
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.

C#
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.

C#
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.

C#
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Magnifo
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNo ChildWindow left behind? Pin
CodeWomper14-Feb-12 17:09
CodeWomper14-Feb-12 17:09 
GeneralRe: No ChildWindow left behind? Pin
atik sarker16-Feb-12 5:31
atik sarker16-Feb-12 5:31 
QuestionWhat's this all about? Pin
pbalaga13-Feb-12 23:36
pbalaga13-Feb-12 23:36 
AnswerRe: What's this all about? Pin
atik sarker16-Feb-12 5:29
atik sarker16-Feb-12 5:29 
GeneralMy vote of 1 Pin
Thornik13-Feb-12 23:15
Thornik13-Feb-12 23:15 
GeneralRe: My vote of 1 Pin
pbalaga13-Feb-12 23:38
pbalaga13-Feb-12 23:38 
GeneralRe: My vote of 1 Pin
Thornik13-Feb-12 23:51
Thornik13-Feb-12 23:51 
QuestionWhere's the beef? Pin
Slacker0078-Feb-12 6:20
professionalSlacker0078-Feb-12 6:20 
AnswerRe: Where's the beef? Pin
Nish Nishant8-Feb-12 6:22
sitebuilderNish Nishant8-Feb-12 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.