【问题标题】:How to identify a Button or title bar Close button click raised Window_Close event in WPF?如何识别按钮或标题栏关闭按钮单击 WPF 中引发的 Window_Close 事件?
【发布时间】:2018-11-08 14:11:11
【问题描述】:

我有 3 个 WPF 窗体,即 MainWindow、SettingWindow 和 FinalWindow。当我单击要克隆当前 SettingWindows 并想要打开 FinalWindow 的按钮时,我在 SettingWindow 中有一个 FinalButton。同样,当我单击标题栏中的关闭按钮时,我想关闭当前窗体并打开 MainWindow。但是我一直面临一个问题,问题是当我单击标题栏中的关闭按钮或单击 ButtonFinal 时,在这两种情况下,MainWindow 都会打开,为了避免这种情况,我必须在关闭事件中添加一个条件才能知道哪个按钮引发了事件。我没有得到问题的实际解决方案。你能帮我解决这个问题吗?

private void ButtonFinal_Click(object o, RoutedEventArgs e)
{
    FinalWindow finalWindow = new FinalWindow();
    finalWindow.Show();
    this.Close();
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    MainWindow mainWindow = new MainWindow();
    mainWindow.Show();
}

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您可以在ButtonFinal_Click 事件处理程序中设置一个标志并在Closing 事件处理程序中检查此标志,例如:

    private bool _closedByButton;
    private void ButtonFinal_Click(object o, RoutedEventArgs e)
    {
        FinalWindow finalWindow = new FinalWindow();
        finalWindow.Show();
        _closedByButton = true;
        this.Close();
        _closedByButton = false;
    }
    
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (_closedByButton)
        {
            //...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 2018-10-08
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多