Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

WPF Main and Child Window Management

, 8 Feb 2012
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 N Tier Solution Ltd
Bangladesh Bangladesh
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralNo ChildWindow left behind? PinmemberCodeWomper14-Feb-12 17:09 
GeneralRe: No ChildWindow left behind? Pinmemberatik sarker16-Feb-12 5:31 
QuestionWhat's this all about? PinmemberPaul8913-Feb-12 23:36 
AnswerRe: What's this all about? Pinmemberatik sarker16-Feb-12 5:29 
GeneralMy vote of 1 PinmemberMember 308248713-Feb-12 23:15 
GeneralRe: My vote of 1 PinmemberPaul8913-Feb-12 23:38 
GeneralRe: My vote of 1 PinmemberMember 308248713-Feb-12 23:51 
QuestionWhere's the beef? PinmemberSlacker0078-Feb-12 6:20 
AnswerRe: Where's the beef? PinmvpNish Sivakumar8-Feb-12 6: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 | Mobile
Web01 | 2.8.140921.1 | Last Updated 8 Feb 2012
Article Copyright 2012 by atik sarker
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid