Search code examples
c#wpf

Closing a window in WPF


I want to close a window in WPF after clicking a button that sends me to another window.

I tried with win3.Close(); but it doesn't work. This is the Main Window that references to the second window.

private void Button_Click_1(object sender, RoutedEventArgs e)
 {
   Window2 win3 = new Window2();
   win3.Show();
 }

Or it should be hidden?


Solution

  • You only need to input this.Close(); before showing next window.

    private void Button_Click_1(object sender, RoutedEventArgs e)
    
     {
       Window2 win3 = new Window2();
       this.Close();
       win3.Show();
     }