Home > other >  WPF minimized owned windows should stay minimized, if Parent is minimized and then restored
WPF minimized owned windows should stay minimized, if Parent is minimized and then restored

Time:07-22

I have a Main window and a child window. The owner of the child window is main window. The child window is not a dialog. I have used the following code in the constructor of the child window:

this.Owner = Application.Current.MainWindow;
this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
this.ShowInTaskbar = false;

I have set this.ShowInTaskbar = false because I want the child window to be displayed in the bottom of the screen when minimized(and not in the taskbar). When I minimize the Main window, child window should also minimize(this is working). But when the child window is already minimized and then if I minimize and restore the main window the child window is also restoring. I want the child window to stay minimized if it was already minimized.

CodePudding user response:

This behavior is by design but you can override it by checking for the child's WindowState property in the parent's StateChanged event. Set a flag if it was minimized, and manually minimize the child window when it's going to switch to normal while the flag is set.

In this sample I put a button for creating and opening a new child Window, and set the child's properties and event listeners in the parent class. So there are no XAML and code-behind files for the child window. The code looks like this:

XAML:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="407,157,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

Code-Behind:

public partial class MainWindow : Window
{
    private Window childWindow;
    private bool ignoreStateChange = false;

    public MainWindow()
    {
        InitializeComponent();
        this.StateChanged  = MainWindow_StateChanged;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        childWindow = new Window() { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner, ShowInTaskbar = false };
        childWindow.StateChanged  = ChildWindow_StateChanged;
        childWindow.Show();
    }


    private void MainWindow_StateChanged(object sender, EventArgs e)
    {
        if (WindowState == WindowState.Normal && childWindow?.WindowState == WindowState.Minimized)
            ignoreStateChange = true;
    }

    private void ChildWindow_StateChanged(object sender, EventArgs e)
    {
        if (ignoreStateChange)
        {
            ignoreStateChange = false;
            childWindow.WindowState = WindowState.Minimized;
            return;
        }
    }
}
  • Related