简体   繁体   English

WPF 最小化拥有的窗口应该保持最小化,如果 Parent 被最小化然后恢复

[英]WPF minimized owned windows should stay minimized, if Parent is minimized and then restored

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).我设置this.ShowInTaskbar = false因为我希望子窗口在最小化时显示在屏幕底部(而不是在任务栏中)。 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.如果子窗口已经最小化,我希望它保持最小化。

This behavior is by design but you can override it by checking for the child's WindowState property in the parent's StateChanged event.此行为是设计使然,但您可以通过在父的StateChanged事件中检查子的WindowState属性来覆盖它。 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.在此示例中,我放置了一个用于创建和打开新子Window的按钮,并在父类中设置子的属性和事件侦听器。 So there are no XAML and code-behind files for the child window.因此,子窗口没有 XAML 和代码隐藏文件。 The code looks like this:代码如下所示:

XAML : 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM