The World’s Largest Online Community for Developers

'; c# - How do I open a second window from the first window in WPF? - LavOzs.Com

I am new to WPF. I have two windows, such as window1 and window2. I have one button in window1. If I click that button, the window2 has to open. What should I do for that?

Here is the code I tried:;

Write your code in window1.

private void Button_Click(object sender, RoutedEventArgs e)
    window2 win2 = new window2();

When you have created a new WPF application you should have a .xaml file and a .cs file. These represent your main window. Create an additional .xaml file and .cs file to represent your sub window.


<Window x:Class="WpfApplication2.MainWindow"
    Title="MainWindow" Height="350" Width="525">
        <Button Content="Open Window" Click="ButtonClicked" Height="25" HorizontalAlignment="Left" Margin="379,264,0,0" Name="button1" VerticalAlignment="Top" Width="100" />


public partial class MainWindow : Window
    public MainWindow()

    private void ButtonClicked(object sender, RoutedEventArgs e)
        SubWindow subWindow = new SubWindow();

Then add whatever additional code you need to these classes:

private void button1_Click(object sender, RoutedEventArgs e)
    window2 win2 = new window2();

Assuming the second window is defined as public partial class Window2 : Window, you can do it by:

Window2 win2 = new Window2();

This helped me: The Owner method basically ties the window to another window in case you want extra windows with the same ones.

LoadingScreen lc = new LoadingScreen();
lc.Owner = this;

Consider this as well.

this.WindowState = WindowState.Normal;

In WPF we have a couple of options by using the Show() and ShowDialog() methods.

Well, if you want to close the opened window when a new window gets open then you can use the Show() method:

Window1 win1 = new Window1();

ShowDialog() also opens a window, but in this case you can not close your previously opened window.

You will need to create an instance of a new window like so.

var window2 = new Window2();

Once you have the instance you can use the Show() or ShowDialog() method depending on what you want to do.



var result = window2.ShowDialog();

ShowDialog() will return a Nullable<bool> if you need that.

You can create a button in window1 and double click on it. It will create a new click handler, where inside you can write something like this:

var window2 = new Window2();

You can use this code:

private void OnClickNavigate(object sender, RoutedEventArgs e)
    NavigatedWindow navigatesWindow = new NavigatedWindow();
What is the correct way to create a single-instance WPF application?
Case insensitive 'Contains(string)'
When to use struct?
Proper use of the IDisposable interface
How do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?
How do I exit a WPF application programmatically?
Page vs Window in WPF?
Close Window after open another window?
return to previous window on WPF