Minimize a main WPF window with it is sub-windows when click on the main window icon in the task bar

Sana'a Al-shboul 0 Reputation points
2023-12-20T14:50:18.6833333+00:00

Assume that I have an application which includes a main WPF window with sub-windows. It means that minimizing the main window by clicking on its icon in the task bar shall also minimize or hide the sub-windows and maximizing the main window shall show the sub-windows on top of the main window.

I use the Activate event of the main window to set the TopMost property to true for all sub- windows when activating the main window by clicking on it. But when I click on the main window icon in the task bar while one of the sub-windows is active then the main window becomes not active and i need to click another time on the icon to minimize the main window with the sub-windows. It means that WasRestoredFromTaskbarIcon() returns false (the same as if the main window is activated by clicking on it) for the first click on the icon and true for the second time.

Problem: Minimizing the main window by clicking on its icon in the task bar shall be repeated twice to minimize the main window with its sub-windows when one of the sub-windows is active and the main window is not active.

I am looking to achieve these point in my application:

1- If the main window and its sub-windows are shown and the user clicks on the main window icon in the task bar shall minimize the main window and its sub-windows (regarding if the main window state is active or not)

2- Showing the main window shall shows the sub-windows on top of the main window

3- Activating the main window by clicking on it shall keeps the sub-windows on top of the main window.

4- Deactivating the main window by clicking on one of the sub-windows shall keep the sub-windows on top of the main window

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
{count} votes

1 answer

Sort by: Most helpful
  1. gekka 6,931 Reputation points MVP
    2023-12-20T17:53:50.28+00:00

    Wouldn't the behavior you want be achieved by setting the Owner property instead of setting the Topmost property for sub-window?

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            //InitializeComponent();
            this.Title = "MainWindow";
            this.Width = 400;
            this.Height = 400;
            this.Background = Brushes.LimeGreen;
            this.ShowInTaskbar = true;
                
            this.Loaded += MainWindow_Loaded;
        }
    
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Window subWindow = new Window();
            subWindow.Title = "SubWindow";
            subWindow.Width =300;
            subWindow.Height = 100;
            subWindow.Background = Brushes.Yellow;
            subWindow.ShowInTaskbar = false;
            //subWindow.Topmost = true; // not set Topmost
            subWindow.Owner = this; // set owner
    
            subWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            subWindow.Show();
        }
    }