How to open a second window from the first window in wpf?

I am new to wpf.I have two windows like window1 and window2. I have one button in window1. if I click this button, a window2 will open. what should i do for this. Here is the code I tried.

window2.show(); 
+69
c # wpf
Jun 21 '12 at 8:05
source share
9 answers

Write your code in window1 .

 private void Button_Click(object sender, RoutedEventArgs e) { window2 win2 = new window2(); win2.Show(); this.Close(); } 
+149
Jun 21 2018-12-12T00:
source share

You need to create a new WPF application. After that you should have a .xaml file and a .cs file. They represent your main window. Create an additional .xaml and .cs file to present your additional 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 the extra code needed for these classes:

SubWindow.xaml SubWindow.xaml.cs

+25
Jun 21 '12 at 8:32
source share
 private void button1_Click(object sender, RoutedEventArgs e) { window2 win2 = new window2(); win2.Show(); } 
+14
Jun 21 '12 at 8:11
source share

In WPF, we have a couple of options using the Show () and ShowDialog () method. Well, if you want to close an open window when a new window opens, you can use the Show () method

 Window1 win1 = new Window1(); win1.Show(); win1.Close(); 

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

+4
Jun 21 2018-12-12T00:
source share

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

 Window2 win2 = new Window2(); win2.Show(); 
+2
Jun 21 '12 at 8:10
source share

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

 var window2 = new Window2(); window2.Show(); 
+2
Jun 21 '12 at 8:11
source share

You will need to instantiate a new window.

 var window2 = new Window2(); 

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

 window2.Show(); 

or

 var result = window2.ShowDialog(); 

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

+2
Aug 20 '15 at 18:48
source share

It helped me: Owner method basically binds a window to another window if you need additional windows with the same.

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

Think about it.

 this.WindowState = WindowState.Normal; this.Activate(); 
+2
Oct 07 '15 at 22:51
source share

you can use this code hopefully help you

 private void OnClickNavigate(object sender, RoutedEventArgs e) { NavigatedWindow navigatesWindow = new NavigatedWindow(); navigatesWindow.ShowDialog(); } 

Thank:)

+1
Jan 24 '14 at 14:27
source share



All Articles