How to create two windows in WPF?
In WPF, you can create two windows using the following steps:
- Create a new WPF application project.
- Add a new WPF window to the project. Right-click on the project folder, select “Add”->”New Item”->”WPF Window”.
- Design the UI interface in the XAML file of the window.
- In the entry point of the application (typically the App.xaml.cs file), create two instances of windows and launch the main window.
Here is an example code:
The code file for the main window in the application is MainWindow.xaml.cs.
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
The code-behind file for SecondaryWindow.xaml is SecondaryWindow.xaml.cs.
using System.Windows;
namespace WpfApp1
{
public partial class SecondaryWindow : Window
{
public SecondaryWindow()
{
InitializeComponent();
}
}
}
Application.xaml.cs:
using System.Windows;
namespace WpfApp1
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mainWindow = new MainWindow();
SecondaryWindow secondaryWindow = new SecondaryWindow();
mainWindow.Show();
secondaryWindow.Show();
}
}
}
This creates a WPF application with two windows – a main window called MainWindow and a secondary window called SecondaryWindow. Both windows will be displayed simultaneously when the application is launched.