Skip to main content

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




Comments

Popular posts from this blog

WPF datagrid cell textbox change event

Entity/Class: public class FeesDetails : INotifyPropertyChanged { public int Id { get; set; } public string FeesName { get; set;} public string FeesDetailsName { get; set; } public int? PaidAmount { get; set; } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(System.String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public int feesAmount { get; set; } public int FeesAmount { get { return this.feesAmount; } set { if (value != this.feesAmount) { this.feesAmount = value; NotifyPropertyChanged("FeesAmount"); } } } } XAML: <DataGrid AutoGenerateColumns="False" Height="21...

mvc razor textboxfor change event change another textboxfor value

Based on value of Weight, Rate , CNF & AWB it will change the value of Freight , TTLCNF anfd TTLFright . Freight= Weight*Rate; TTLCNF  = Weight*CNF; TTLFright=  Freight+ TTLCNF  + AWB; @Html.TextBoxFor(model => model.Weight, new { onChange="return GetWight(this);"}) @Html.TextBoxFor(model => model.Rate, new { onChange="return GetWight(this);"})/Kg @Html.TextBoxFor(model => model.Freight, new {disabled = "disabled" , @readonly = "readonly" ,onChange="return GetTTLFright(this);"}) @Html.TextBoxFor(model => model.CNFPK, new { onChange="return GetCNFPK(this);"}) @Html.TextBoxFor(model => model.TTLCNF, new {disabled = "disabled" , @readonly = "readonly",onChange="return GetTTLFright(this);" }) @Html.TextBoxFor(model => model.AWB, new { onChange="return GetTTLFright(this);"}) and script <script> function GetW...

mvvm double click event in listview

If you want to get the double click event on a listview item you can try with this code; <ListView Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Width="250" Height="200" HorizontalAlignment="Stretch" VerticalAlignment="Top" AlternationCount="2" BorderBrush="#FFA8CC7B" ItemContainerStyle="{StaticResource alternatingStyle}" ItemsSource="{Binding FromPayerNameList}" SelectedItem="{Binding SelectedFromPayer, Mode=TwoWay}"> <ListView.ItemTemplate> <DataTemplate> <TextBlock Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" Text=...