该程序有一个MainWindow,带有用于打开Child Windows的按钮。当其中一个孩子显示消息框时,当我关闭孩子时,主窗口最小化。
protected void EventBtn_Click(object sender, RoutedEventArgs e)
{
Child child = new child();
child.Show();
child.Owner = this;
}
解决方案:
感谢@Oscar Martinez
子:
public partial class Child: Window
{
public Child()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello world!");
}
// this is what you need to add
protected override void OnClosing(CancelEventArgs e)
{
this.Owner = null;
}
}
父:
public partial class Parent: Window
{
public Parent()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Child child = new Child();
child.Show();
child.Owner = this;
}
}
答案 0 :(得分:0)
我也试过了,但是像Shahrooz一样,它永远不会发生。
试试这个,也许它可以帮到你:
protected void EventBtn_Click(object sender, RoutedEventArgs e)
{
using (Child child = new Child())
{
child.ShowDialog();
}
}
答案 1 :(得分:0)
我有同样的行为。通过处理Closing
表单的Child
事件很容易解决。
子:
public partial class Child: Window
{
public Child()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello world!");
}
// this is what you need to add
private void Child_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Owner = null;
}
}
父:
public partial class Parent: Window
{
public Parent()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Child child = new Child();
child.Show();
child.Owner = this;
}
}
如果您最小化Parent,则Child也会最小化。
如果您关闭Child,则Parent不会最小化。
答案 2 :(得分:0)
父窗口没有最小化,它只是回到其他应用程序。要避免它,请设置父窗口的最顶层属性。
public ParentWindow()
{
InitializeComponent();
this.Topmost = true;
}
然后,按如下方式创建子窗口。
private void button_Click(object sender, RoutedEventArgs e)
{
Child child = new Child();
child.Owner = this;
child.Show();
}