如何在WPF窗口关闭后调用方法?

时间:2015-09-24 15:00:31

标签: c# .net wpf

我有几个控件链接如下:

ProductSearchControl (UserControl) [contains SearchProducts()]
     --ProductListControl (UserControl)
          --ProductResultPanel(UserControl)
                  |
                  |
                  |
           ProductDetailsWindow (Window)

ProductResultPanel.xaml.cs 中,在按钮点击时调用以下方法。

    void OnModifyPrice(object sender, RoutedEventArgs e)
    {
        if (this.ModifyPrice != null)
        {
            ProductDetailsWindow win = new ProductDetailsWindow(this.productId);
            win.ShowInTaskbar = false;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            win.Owner = Window.GetWindow(this);
            bool? result = win.ShowDialog();
        }
    }

ProductDetailsWindow 上,如果我点击按钮,我想在 ProductSearchControl 中执行 SearchProducts(),但我目前有困难。

ProductDetailsWindow.xaml.cs

    private void OnCancel(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
    }

    private void OnSave(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
    }

因此,点击“保存”按钮后,我将返回 ProductResultPanel ,但我不知道如何调用 SearchProducts()

这可能吗?如果可以的话,任何帮助都会受到赞赏吗?

1 个答案:

答案 0 :(得分:1)

Window即使在您关闭之后仍然存在,您可以从ProductResultPanel.xaml.cs访问其公共属性和方法。例如:

var x = win.SomePublicProperty;
var y = win.GetProducts();
// whatever you need and provide access to in ProductDetailsWindow