【发布时间】:2013-10-09 23:38:30
【问题描述】:
以下代码演示了我遇到的一个问题,即关闭子窗口会最小化父窗口,这是我不希望发生的。
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 是主应用程序窗口。
SomeDialog 是在 Window1 中的某个事件上弹出的窗口(在示例中双击 window1),需要 无模式。
CustomMessageBox 是一个在“SomeDialog”(示例中双击 SomeDialog)中需要modal的事件时弹出的窗口。
如果您运行应用程序,然后双击 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;
}
}
【问题讨论】:
-
我在子窗口启动一个打开目录的过程时遇到了同样的问题(例如
Process.Start("C:\\"))。接受的答案对我有用。
标签: wpf dialog window minimize