在WPF中为所有者窗口调用窗口加载的事件

时间:2017-04-05 12:02:21

标签: c# wpf

我有windows1和windows2,windows1拥有windows2

_windows2 = new Windows2();
_windows2.Owner = this; // this=windows1
_windows2.Show();
this.Hide();

关闭windows2时,我会显示window1

private void Window2_Closed(object sender, EventArgs e)
{
    this.Owner.Show();
}

但是window1加载的事件没有被调用,我有一些必须在关闭window2后更新的计算。

如何在关闭window2后调用window1加载的事件?

1 个答案:

答案 0 :(得分:1)

您可以将代码移至Activated事件处理程序,或在Loaded事件发生时调用Activated事件处理程序:

int i = 0;
this.Activated += (ss, ee) =>
{
    if (i++ == 0) //not the first time...
        OnLoaded(this, EventArgs.Empty);
};

如果您不希望在应用程序或窗口之间切换时执行代码,您可以将代码直接放在Window2_Closed事件处理程序中:

private void Window2_Closed(object sender, EventArgs e)
{
    this.Owner.Show();
    OnLoaded(this, EventArgs.Empty);
}
相关问题