关闭子窗口最小化父窗口 [英] Closing child window minimizes parent

查看:31
本文介绍了关闭子窗口最小化父窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码演示了我遇到的一个问题,即关闭子窗口会最小化父窗口,这是我不希望发生的.

The following code demonstrates an issue I'm having where closing a child window minimizes the parent window, which I dont want to happen.

    class SomeDialog : Window
    {
        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            new CustomMessageBox().ShowDialog();
        } 
    }

    class CustomMessageBox : Window
    {
        public CustomMessageBox()
        {
            Owner = Application.Current.MainWindow;
        }
    }

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            new SomeDialog() { Owner = this }.Show();
        }
    }

Window1 是主应用程序窗口.

Window1 is the main application window.

SomeDialog 是一个在 Window1 中的某个事件上弹出的窗口(在示例中双击 window1),需要 无模式.

SomeDialog is a window that pops up on some event within Window1(double clicking window1 in the example) that needs to be modeless.

CustomMessageBox 是一个在SomeDialog"(示例中双击 SomeDialog)中需要modal的事件时弹出的窗口.

CustomMessageBox is a window that pops up on some event within "SomeDialog" (double clicking SomeDialog in the example) that needs to be modal.

如果您运行应用程序,然后双击 Window1 的内容以调出 SomeDialog,然后您再双击 SomeDialog 的内容以调出 CustomMessagebox.

If you run the application, and then double click Window1's content to bring up SomeDialog, and then you then double click SomeDialog's content to bring up the CustomMessagebox.

现在您关闭 CustomMessagebox.很好.

Now you close CustomMessagebox. Fine.

现在如果你关闭 SomeDialog,Window1 会最小化吗?为什么它会最小化,我该如何阻止它?

Now if you close SomeDialog, Window1 minimizes? Why is it minimizing and how can I stop it?

编辑:看起来解决方法相当简单,使用 Viv 建议的技术.

Edit : It appears the workaround is rather simple, using the technique suggesrted by Viv.

class SomeDialog : Window
    {
        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            new CustomMessageBox().ShowDialog();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            Owner = null;
        }
    }

推荐答案

为什么会最小化,我该如何阻止它?

Why is it minimizing and how can I stop it?

不确定为什么"也许您可以将其报告为错误并查看他们回复的内容,就像您不希望发生这种情况的非模态对话框一样.

Not sure about the "Why" maybe you can report it as a bug and see what they reply with as with a non-modal dialog you do not expect this to happen.

至于解决方法,请尝试以下方法:

As for a workaround, Try something like this:

public partial class MainWindow : Window {
  ...

  protected override void OnMouseDoubleClick(MouseButtonEventArgs e) {
    base.OnMouseDoubleClick(e);
    var x = new SomeDialog { Owner = this };        
    x.Closing += (sender, args) => {
      var window = sender as Window;
      if (window != null)
        window.Owner = null;
    };
    x.Show();
  }
}

^^ 这应该可以防止 MainWindow(parent) 在 SomeDialog 关闭时最小化.

^^ This should prevent the MainWindow(parent) from minimizing when SomeDialog is closed.

这篇关于关闭子窗口最小化父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆