Tuesday, February 7, 2012

WPF Main and Child Window Management


In our WPF application we call any child window in our event , example as button click event or other click event. When we can a child window from our main window normally it load our main window.  But if we click on that event again it will load again in 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 load once it will not load newly again on it’s event. If we minimize it then it will appear in main window bottom as Task bar. If we click on it’s event it will load from minimize. And only the main window will load on our computer Taskbar . Their will no child window loaded on Taskbar.
I have create a method name showWindow. Let see how the method working:
This foreach loop find our all current UI from namespace using split if it find out the desire window it just load it on 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 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 desire 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 main window state change event.
For minimize the main window find out all open child window state using for loop and for maximize it set all child window state 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 window we have to set ShowInTaskbar property false. Bcoz 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 Owner  with our main window.

so our final main window and child window will look like




SQL Get all Index create script from Database

To get all script from database as a create new index into another database you can use the following --Get all Index Script SELECT...