返回到 WPF 上的前一个窗口 [英] return to previous window on WPF

查看:20
本文介绍了返回到 WPF 上的前一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WPF 的新手,找不到这个问题的答案:

I'm new to WPF and couldn't find an answer to to this issue:

我想在 3 个窗口之间导航-<代码>主窗口 ->窗口 1 ->Window2

I have 3 windows I want to navigate between- MainWindow -> Window1 -> Window2

cancel按钮上点击Window2我想回到Window1.

On cancel button click on Window2 I want to return to Window1.

我发现此代码可以在 2 个窗口之间导航,但不能根据需要在 3 个窗口之间导航:

I found this code to navigate between 2 windows, but not between 3 as I need:

主窗口:

    private void Window1_Click(object sender, RoutedEventArgs e)
    {
        Window1 window1 = new Window1();
        window1.Show();
        this.Hide();
    }

窗口 1:

    private void btn_Cancel_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.MainWindow.Show();
        this.Close();
    }

    private void btn_Window2_Click(object sender, RoutedEventArgs e)
    {
        Window2 window2 = new Window2();
        window2 .Show();
        this.Hide();
    }

窗口 2:

    private void btn_Cancel_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
        //what should I write to show Window1 again?
    }

推荐答案

像这样更改显示窗口的方式:

Change the way you show your windows like this:

private void Window1_Click(object sender, RoutedEventArgs e)
{
    Hide();
    new Window1().ShowDialog();
    ShowDialog();
}

并使用 DialogResult 属性隐藏您的窗口(主窗口除外):

And use the DialogResult property to hide your windows (except the main window):

private void btn_Cancel_Click(object sender, RoutedEventArgs e)
{
    DialogResult = false;
}

这篇关于返回到 WPF 上的前一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆