Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create 2 separate windows in WPF with C#

Tags:

c#

windows

wpf

I want to create 2 separate windows in xaml and I want to control them separately from the code part. Do you have any idea how to do that ? If you can provide some code examples, I will be appreciated.

Thank you from now...

like image 363
Samet Avatar asked Dec 23 '11 14:12

Samet


People also ask

How do I navigate to another window in WPF?

NavigationService is for browser navigation within WPF. What you are trying to do is change to a different window TrainingFrm . To go to a different window, you should do this: private void conditioningBtn_Click(object sender, RoutedEventArgs e) { var newForm = new TrainingFrm(); //create your new form.

Is WPF still relevant 2021?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).


1 Answers

Add a second Window (the first one being MainWindow.xaml) in your project (right click your project-> Add -> Window). Let's call it BobbyWindow.

In the constructor of MainWindow.xaml.cs, call:

BobbyWindow bWin = new BobbyWindow();
bwin.Owner = this;
bWin.Show(); 

voila.

EDIT: additional info to reflect the comments

The main difference between this:

public MainWindow() 
{ 
   InitializeComponent(); 
   Window1 bWin = new Window1(); 
   bWin.Owner = this; 
   bWin.Show(); 
}

And that:

Window1 bWin = new Window1(); 

public MainWindow() 
    { 
       InitializeComponent(); 

       bWin.Owner = this; 
       bWin.Show(); 
    }

Is that in the first case, bWin is local to the MainWindow() constructor, which means it only exists within the brackets of MainWindow().
In the second case, bWin is local to the class, which means it is only accessible from within the boundaries of the MainWindow class

like image 50
Louis Kottmann Avatar answered Sep 30 '22 07:09

Louis Kottmann