Introduction
In this blog, we will learn basics of Windows in WPF and how create a Window in WPF using C#.
Follow the below steps to create a new window in WPF:
Step 1: Create an empty WPF using Visual Studio, enter the name for the application and click on OK.
Step 2: Create a button using the following code or drag and drop a button from the ToolBox of Visual Studio 2013.
Code:
- <Button Content="Button" HorizontalAlignment="Left" Margin="133,120,0,0" VerticalAlignment="Top" Width="75"/>
Step 3: Solution Name(Right Click) --> Add à New Window.
Step 4: Name the New window and click on OK.
Step 5: Name the New window and click on OK.
Code:
- <TextBox HorizontalAlignment="Left" Height="23" Margin="119,98,0,0" TextWrapping="Wrap"Text="TextBox" VerticalAlignment="Top" Width="120"/>
Rename the Text if you need
Now move to MainWindow.xaml and double click on the Button, enter the following code
- Newwindow p = Newwindow();
-
- p.Show();
Code for Mainwindow.xaml.cs is:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace newwindow.xaml
- {
-
-
-
- public partial class MainWindow: Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- Newwindow p = Newwindow();
- p.Show();
- }
- }
- }
Now run the program.