Click here to Skip to main content
11,481,404 members (64,550 online)
Click here to Skip to main content

Tagged as

WPF Main and Child Window Management

, 8 Feb 2012 CPOL 28.4K 1.5K 10
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

atik sarker
Software Developer Magnifo
Bangladesh Bangladesh
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralNo ChildWindow left behind? Pin
CodeWomper14-Feb-12 18:09
memberCodeWomper14-Feb-12 18:09 
GeneralRe: No ChildWindow left behind? Pin
atik sarker16-Feb-12 6:31
memberatik sarker16-Feb-12 6:31 
QuestionWhat's this all about? Pin
Paul8914-Feb-12 0:36
memberPaul8914-Feb-12 0:36 
AnswerRe: What's this all about? Pin
atik sarker16-Feb-12 6:29
memberatik sarker16-Feb-12 6:29 
GeneralMy vote of 1 Pin
Member 308248714-Feb-12 0:15
memberMember 308248714-Feb-12 0:15 
GeneralRe: My vote of 1 Pin
Paul8914-Feb-12 0:38
memberPaul8914-Feb-12 0:38 
GeneralRe: My vote of 1 Pin
Member 308248714-Feb-12 0:51
memberMember 308248714-Feb-12 0:51 
QuestionWhere's the beef? Pin
Slacker0078-Feb-12 7:20
memberSlacker0078-Feb-12 7:20 
AnswerRe: Where's the beef? Pin
Nish Sivakumar8-Feb-12 7:22
mvpNish Sivakumar8-Feb-12 7:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 8 Feb 2012
Article Copyright 2012 by atik sarker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid