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

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

问题描述

以下代码演示了我在关闭子窗口时最小化父窗口的问题,我不想发生。

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

class CustomMessageBox:Window
{
public CustomMessageBox()
{
Owner = Application.Current。主窗口;
}
}

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

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

Window1是主要的应用程序窗口。 SomeDialog是一个窗口,它会在Window1(在示例中双击window1)中的一些事件上弹出,需要 modeless



p>

CustomMessageBox是一个窗口,弹出一些需要 modal 的SomeDialog(双击SomeDialog)中的某个事件。 p>

如果您运行应用程序,然后双击Window1的内容以启动SomeDialog,然后双击SomeDialog的内容来启动CustomMessagebox。



现在关闭CustomMessagebox。



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



编辑:使用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;
}
}


解决方案


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


不知道为什么也许你可以报告它作为一个错误,并看到他们回复与一个非模态对话,你不希望这样会发生。



至于解决方法,尝试这样的东西:

  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 (父)从 SomeDialog 关闭时最小化


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 is the main application window.

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

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

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.

Now you close CustomMessagebox. Fine.

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

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();
  }
}

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

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

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