---
title: "Create a new app with Visual Studio tutorial"
description: Follow this tutorial to learn how to create a new WPF app for .NET with Visual Studio 2019.
ms.date: 01/18/2021
ms.topic: tutorial
dev_langs:
- "csharp"
- "vb"
---
# Tutorial: Create a new WPF app (WPF .NET)
In this short tutorial, you'll learn how to create a new Windows Presentation Foundation (WPF) app with Visual Studio. Once the initial app has been generated, you'll learn how to add controls and how to handle events. By the end of this tutorial, you'll have a simple app that adds names to a list box.
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
In this tutorial, you learn how to:
> [!div class="checklist"]
>
> - Create a new WPF app
> - Add controls to a form
> - Handle control events to provide app functionality
> - Run the app
Here's a preview of the app you'll build while following this tutorial:
:::image type="content" source="media/create-app-visual-studio/app-finished.png" alt-text="Finished sample app for WPF tutorial":::
## Prerequisites
- [Visual Studio 2019 version 16.8.4 or later versions](https://visualstudio.microsoft.com/downloads/?utm_medium=microsoft&utm_source=docs.microsoft.com&utm_campaign=inline+link&utm_content=download+vs2019+desktopguide+wpf)
- Select the [Visual Studio Desktop workload](/visualstudio/install/modify-visual-studio?view=vs-2019&preserve-view=true#modify-workloads)
- Select the [.NET 5 individual component](/visualstudio/install/modify-visual-studio?view=vs-2019&preserve-view=true#modify-individual-components)
## Create a WPF app
The first step to creating a new app is opening Visual Studio and generating the app from a template.
01. Open Visual Studio.
01. Select **Create a new project**.
:::image type="content" source="media/create-app-visual-studio/vs-create-new-project.png" alt-text="Create a new WPF project in Visual Studio 2019 for .NET":::
01. In the **Search for templates** box, type **wpf**, and then press Enter.
01. In the **code language** dropdown, choose **C#** or **Visual Basic**.
01. In the templates list, select **WPF Application** and then select **Next**.
> [!IMPORTANT]
> Don't select the **WPF Application (.NET _Framework_)** template.
:::image type="content" source="media/create-app-visual-studio/vs-template-search-16.8.png" alt-text="Search for the WPF template in Visual Studio 2019 for .NET":::
01. In the **Configure your new project** window, do the following:
01. In the **Project name** box, enter **Names**.
01. Select the **Place solution and project in the same directory** check box.
01. Optionally, choose a different **Location** to save your code.
01. Select the **Create** button.
:::image type="content" source="media/create-app-visual-studio/vs-config-new-project-16.8.png" alt-text="Configure new WPF project in Visual Studio 2019 for .NET":::
Once the app is generated, Visual Studio should open the XAML designer pane for the default window, _MainWindow_. If the designer isn't visible, double-click on the _MainWindow.xaml_ file in the **Solution Explorer** pane to open the designer.
### Important parts of Visual Studio
Support for WPF in Visual Studio has five important components that you'll interact with as you create an app:
:::image type="content" source="media/create-app-visual-studio/vs-main-window.png" alt-text="The important components of Visual Studio you should know when creating a WPF project for .NET":::
01. Solution Explorer
All of your project files, code, windows, resources, will appear in this pane.
02. Properties
This pane shows property settings you can configure based on the item selected. For example, if you select an item from **Solution Explorer**, you'll see property settings related to the file. If you select an object in the **Designer**, you'll see settings for that item.
03. Toolbox
The toolbox contains all of the controls you can add to a form. To add a control to the current form, double-click a control or drag-and-drop the control.
04. XAML designer
This is the designer for a XAML document. It's interactive and you can drag-and-drop objects from the **Toolbox**. By selecting and moving items in the designer, you can visually compose the user interface (UI) for your app.
When both the designer and editor are visible, changes to one is reflected in the other. When you select items in the designer, the **Properties** pane displays the properties and attributes about that object.
05. XAML code editor
This is the XAML code editor for a XAML document. The XAML code editor is a way to craft your UI by hand without a designer. The designer may infer the values of properties on a control when the control is added in the designer. The XAML code editor gives you a lot more control.
When both the designer and editor are visible, changes to one is reflected in the other. As you navigate the text caret in the code editor, the **Properties** pane displays the properties and attributes about that object.
## Target .NET 5.0
After you create your project, change the target framework to .NET 5.0. Double-click on the _Names_ project file in the **Solution Explorer**. This opens up the project file for editing. Set the `` element to `net5.0-windows`:
```xml
net5.0-windows
```
Save the project file and then close the editor tab.
## Examine the XAML
After your project is created, the XAML code editor is visible with a minimal amount of XAML code to display the window. If the editor isn't open, double-click the _MainWindow.xaml_ item in the **Solution Explorer**. You should see XAML similar to the following:
```xaml
```
Let's break down this XAML code to understand it better. XAML is simply XML that can be processed by the compilers that WPF uses. It describes the WPF UI and interacts with .NET code. To understand XAML, you should, at a minimum, be familiar with the basics of XML.
The document root `` represents the type of object being described by the XAML file. There are eight attributes declared, and they generally belong to three categories:
- Namespaces
An XML namespace provides structure to the XML, determining what is or isn't allowed to be declared in the file.
The main `xmlns` attribute imports the XML namespace for the entire file, and in this case, maps to the types declared by WPF. The other XML namespaces declare a prefix and import other types and objects for the XAML file. For example, the `xmlns:local` namespace declares the `local` prefix and maps to the objects declared by your project, the ones declared in the `Names` code namespace.
- `x:Class` attribute
This attribute maps the `` to the type defined by your code: the _MainWindow.xaml.cs_ or _MainWindow.xaml.vb_ file, which is the `Names.MainWindow` class.
- `Title` attribute
Any normal attribute declared on the XAML object sets a property of that object. In this case the `Title` attribute sets the `Window.Title` property.
## Change the window
First, let's run the project and see the default output. You'll see that a window that pops up, without any controls, and a title of **MainWindow**:
:::image type="content" source="media/create-app-visual-studio/app-default.png" alt-text="A blank WPF app":::
For our example app, this window is too large, and the title bar isn't descriptive. Change the title and size of the window by changing the appropriate attributes in the XAML to the following values:
:::code language="xaml" source="snippets/create-app-visual-studio/csharp/Start.xaml" highlight="8":::
## Prepare the layout
WPF provides a powerful layout system with many different layout controls. Layout controls help place and size child controls, and can even do so automatically. The default layout control provided to you in this XAML is the `` control.
The `Grid` control lets you define rows and columns, much like a table, and place controls within the bounds of a specific row and column combination. You can have any number of child controls or other layout controls added to the `Grid`. For example, you can place another `Grid` control in a specific row and column combination, and that new `Grid` can then define more rows and columns and have its own children.
The `` control defines rows and columns in which your controls will be. A grid always has a single row and column declared, meaning, the grid by default is a single cell. That doesn't really give you much flexibility in placing controls.
Before we add the new rows and columns, add a new attribute to the `` element: `Margin="10"`. This insets the grid from the window and makes it look a little nicer.
Next, define two rows and two columns, dividing the grid into four cells:
:::code language="xaml" source="snippets/create-app-visual-studio/csharp/LayoutStep2.xaml" highlight="9-21":::
Select the grid in either the XAML code editor or XAML designer, you'll see that the XAML designer shows each row and column:
:::image type="content" source="media/create-app-visual-studio/vs-designer-grid-rows-columns.png" alt-text="A WPF app with the margin set on a grid":::
## Add the first control
Now that the grid has been created, we can start adding controls to it. First, start with the label control. Create a new `