Home > front end >  What is the previous window called in WPF
What is the previous window called in WPF

Time:11-26

I know the current window can be used with "this" but is there anything I can use to call the previous window?

For example I have this code going off when I press a button

Buyer_Login BuyerWindow = new Buyer_Login();
            Visibility= Visibility.Hidden;
            BuyerWindow.Show();

I need to be able to go back to the first window and I need to close the BuyerWindow and I was going to do it with this.Close();

What can I do to make the first window's visibility visible again?

CodePudding user response:

You could handle the Window.Closed event:

MainWindow.xaml.cs

private void OnClick(object sender, EventArgs e)
{
  var loginWindow = new BuyerLogin();
  loginWindow.Closed  = OnBuyerLoginWindowClosed;
  this.Visibility = Visibility.Hidden;
  loginWindow.Show();
}

private void OnBuyerLoginWindowClosed(object sender, EventArgs e) 
  => this.Visibility = Visibility.Visible;

You should consider to show the login window from the App.xaml.cs before you show your main window (recommended):

App.xaml.cs

private async void App_OnStartup(object sender, StartupEventArgs e)
{
  var loginWindow = new BuyerLogin();
  bool? dialogResult = loginWindow.ShowDialog();
  if (dialogResult.GetValueOrDefault()) 
  {
    var mainWindow = new MainWindow();
    mainWindow.Show();
  }
}

App.xaml

<Application Startup="App_OnStartup">
  <Application.Resources>
  </Application.Resources>
</Application>

CodePudding user response:

There is a collection of open windows.

  App.Current.Windows;

It depends on exactly what you're doing opening windows. If you start up then mainwindow will be [0] in that collection.

Say you then open an instance of window1.

That in turn opens an instance of window2.

There is a bit of a complication if you f5 in visual studio because it opens adorner windows. Setting that aside for a moment.

When I write code to do what I describe above. In Window2 I handle content rendered:

public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();
    }

    private void Window_ContentRendered(object sender, EventArgs e)
    {
        var wins = App.Current.Windows;
        wins[1].Close();
    }
}

That instance of Window1 is closed.

Your new window is very likely the last window in that zero based collection and the previous one the window before that. You could perhaps search the collection and find index for "this" and subtract one if you're doing more complicated things.

The chances are though, you want to close the window indexed by the count of that collection minus 2. Because it's zero based.

With my exploratory code, window1 closes with this:

    private void Window_ContentRendered(object sender, EventArgs e)
    {
        var wins = App.Current.Windows;
        wins[wins.Count - 2].Close();
    }

Personally, I prefer single window apps and switch out the content in part of mainwindow. Leaving navigation buttons etc static in mainwindow. If you're effectively opening one other window and closing the previous to do things then maybe you could consider a single window app instead.

  • Related