152

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:

window2.show();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ASHOK A
  • 1,966
  • 3
  • 17
  • 31

9 Answers9

269

Write your code in window1.

private void Button_Click(object sender, RoutedEventArgs e)
{
    window2 win2 = new window2();
    win2.Show();
}
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
CHANDRA
  • 4,778
  • 8
  • 32
  • 51
53

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.

MainWindow.xaml

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Open Window" Click="ButtonClicked" Height="25" HorizontalAlignment="Left" Margin="379,264,0,0" Name="button1" VerticalAlignment="Top" Width="100" />
    </Grid>
</Window>

MainWindow.xaml.cs

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

    private void ButtonClicked(object sender, RoutedEventArgs e)
    {
        SubWindow subWindow = new SubWindow();
        subWindow.Show();
    }
}

Then add whatever additional code you need to these classes:

SubWindow.xaml
SubWindow.xaml.cs
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
TokyoMike
  • 798
  • 4
  • 16
  • 26
    simple enough! I hate wpf, it's just so uselessly complicated and designed to be counter intutive. – Ren Apr 10 '15 at 19:35
19
private void button1_Click(object sender, RoutedEventArgs e)
{
    window2 win2 = new window2();
    win2.Show();
}
Babak.Abad
  • 2,839
  • 10
  • 40
  • 74
KF2
  • 9,887
  • 8
  • 44
  • 77
6

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

Window2 win2 = new Window2();
win2.Show();
Vladislav Zorov
  • 2,998
  • 19
  • 32
6

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;
lc.Show();

Consider this as well.

this.WindowState = WindowState.Normal;
this.Activate();
5

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();
win1.Show();
win1.Close();

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1399377
  • 469
  • 2
  • 7
  • 19
2

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();
window2.Show();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LadislavM
  • 414
  • 1
  • 6
  • 22
2

You can use this code:

private void OnClickNavigate(object sender, RoutedEventArgs e)
{
    NavigatedWindow navigatesWindow = new NavigatedWindow();
    navigatesWindow.ShowDialog();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ibmstafa
  • 71
  • 1
2

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.

window2.Show();

or

var result = window2.ShowDialog();

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

jsw
  • 175
  • 3
  • 12