--- title: What is Windows Presentation Foundation description: This article gives an overview of WPF with .NET Core and .NET 5. ms.date: 01/14/2021 ms.topic: overview dev_langs: - "csharp" - "vb" #Customer intent: As a developer, I want to understand the components of WPF so that I can understand the overall picture of WPF. --- # Desktop Guide (WPF .NET) Welcome to the Desktop Guide for Windows Presentation Foundation (WPF), a UI framework that is resolution-independent and uses a vector-based rendering engine, built to take advantage of modern graphics hardware. WPF provides a comprehensive set of application-development features that include Extensible Application Markup Language (XAML), controls, data binding, layout, 2D and 3D graphics, animation, styles, templates, documents, media, text, and typography. WPF is part of .NET, so you can build applications that incorporate other elements of the .NET API. [!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)] There are two implementations of WPF: 01. **.NET** version (this guide): An open-source implementation of WPF hosted on [GitHub](https://github.com/dotnet/wpf), which runs on .NET 5 or higher (including .NET Core 3.1). The XAML designer requires, at a minimum, [Visual Studio 2019 version 16.8](https://visualstudio.microsoft.com/downloads/?utm_medium=microsoft&utm_source=docs.microsoft.com&utm_campaign=inline+link&utm_content=download+vs2019+desktopguide+wpf). Even though .NET is a cross-platform technology, WPF isn't and only runs on Windows. 01. **.NET Framework 4** version: The .NET Framework implementation of WPF that's supported by Visual Studio 2019 and Visual Studio 2017. .NET Framework 4 is a Windows-only version of .NET and is considered a Windows Operating System component. This version of WPF is distributed with .NET Framework. For more information about the .NET Framework version of WPF, see [Introduction to WPF for .NET Framework](../../../framework/wpf/introduction-to-wpf.md?view=netframeworkdesktop-4.8&preserve-view=true). This overview is intended for newcomers and covers the key capabilities and concepts of WPF. To learn how to create a WPF app, see [Tutorial: Create a new WPF app](../get-started/create-app-visual-studio.md). ## Why migrate from .NET Framework WPF for .NET 5.0 provides new features and enhancements over .NET Framework. To learn how to migrate an app, see [How to migrate a WPF desktop app to .NET 5](../migration/convert-project-from-net-framework.md). ## Program with WPF WPF exists as a subset of .NET types that are, mostly located in the namespace. If you have previously built applications with .NET with frameworks like ASP.NET and Windows Forms, the fundamental WPF programming experience should be familiar, you: - Instantiate classes - Set properties - Call methods - Handle events WPF includes more programming constructs that enhance properties and events: [dependency properties](../../../framework/wpf/advanced/dependency-properties-overview.md) and [routed events](../../../framework/wpf/advanced/routed-events-overview.md). ## Markup and code-behind WPF lets you develop an application using both *markup* and *code-behind*, an experience with which ASP.NET developers should be familiar. You generally use XAML markup to implement the appearance of an application while using managed programming languages (code-behind) to implement its behavior. This separation of appearance and behavior has the following benefits: - Development and maintenance costs are reduced because appearance-specific markup isn't tightly coupled with behavior-specific code. - Development is more efficient because designers can implement an application's appearance simultaneously with developers who are implementing the application's behavior. - [Globalization and localization](../../../framework/wpf/advanced/wpf-globalization-and-localization-overview.md) for WPF applications is simplified. ### Markup XAML is an XML-based markup language that implements an application's appearance declaratively. You typically use it to define windows, dialog boxes, pages, and user controls, and to fill them with controls, shapes, and graphics. The following example uses XAML to implement the appearance of a window that contains a single button: ```xaml ``` Specifically, this XAML defines a window and a button by using the `Window` and `Button` elements. Each element is configured with attributes, such as the `Window` element's `Title` attribute to specify the window's title-bar text. At run time, WPF converts the elements and attributes that are defined in markup to instances of WPF classes. For example, the `Window` element is converted to an instance of the class whose property is the value of the `Title` attribute. The following figure shows the user interface (UI) that is defined by the XAML in the previous example: :::image type="content" source="media/index/markup-window-button.png" alt-text="A window that contains a button"::: Since XAML is XML-based, the UI that you compose with it's assembled in a hierarchy of nested elements that is known as an [element tree](../../../framework/wpf/advanced/trees-in-wpf.md). The element tree provides a logical and intuitive way to create and manage UIs. ### Code-behind The main behavior of an application is to implement the functionality that responds to user interactions. For example clicking a menu or button, and calling business logic and data access logic in response. In WPF, this behavior is implemented in code that is associated with markup. This type of code is known as code-behind. The following example shows the updated markup from the previous example and the code-behind: ```xaml ``` The updated markup defines the `xmlns:x` namespace and maps it to the schema that adds support for the code-behind types. The `x:Class` attribute is used to associate a code-behind class to this specific XAML markup. Considering this attribute is declared on the `` element, the code-behind class must inherit from the `Window` class. ```csharp using System.Windows; namespace SDKSample { public partial class AWindow : Window { public AWindow() { // InitializeComponent call is required to merge the UI // that is defined in markup with this class, including   // setting properties and registering event handlers InitializeComponent(); } void button_Click(object sender, RoutedEventArgs e) { // Show message box when button is clicked. MessageBox.Show("Hello, Windows Presentation Foundation!"); } } } ``` ```vb Namespace SDKSample Partial Public Class AWindow Inherits System.Windows.Window Public Sub New() ' InitializeComponent call is required to merge the UI ' that is defined in markup with this class, including   ' setting properties and registering event handlers InitializeComponent() End Sub Private Sub button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) ' Show message box when button is clicked. MessageBox.Show("Hello, Windows Presentation Foundation!") End Sub End Class End Namespace ``` `InitializeComponent` is called from the code-behind class's constructor to merge the UI that is defined in markup with the code-behind class. (`InitializeComponent` is generated for you when your application is built, which is why you don't need to implement it manually.) The combination of `x:Class` and `InitializeComponent` ensure that your implementation is correctly initialized whenever it's created. Notice that in the markup the ` ``` Because this style targets all controls, the style is automatically applied to all the buttons in the window, as shown in the following figure: :::image type="content" source="media/index/styles-buttons.png" alt-text="Two orange buttons"::: For more information, see [Styles and templates](../controls/styles-templates-overview.md). ### Resources Controls in an application should share the same appearance, which can include anything from fonts and background colors to control templates, data templates, and styles. You can use WPF's support for user interface resources to encapsulate these resources in a single location for reuse. The following example defines a common background color that is shared by a and a : ```xaml ``` For more information, see [How to define and reference a WPF resource](../systems/xaml-resources-how-to-define-and-reference.md). ### Custom controls Although WPF provides a host of customization support, you may encounter situations where existing WPF controls do not meet the needs of either your application or its users. This can occur when: - The user interface that you require cannot be created by customizing the look and feel of existing WPF implementations. - The behavior that you require isn't supported (or not easily supported) by existing WPF implementations. At this point, however, you can take advantage of one of three WPF models to create a new control. Each model targets a specific scenario and requires your custom control to derive from a particular WPF base class. The three models are listed here: - **User Control Model**\ A custom control derives from and is composed of one or more other controls. - **Control Model** A custom control derives from and is used to build implementations that separate their behavior from their appearance using templates, much like most WPF controls. Deriving from allows you more freedom for creating a custom user interface than user controls, but it may require more effort. - **Framework Element Model**.\ A custom control derives from when its appearance is defined by custom rendering logic (not templates). For more information on custom controls, see [Control authoring overview](../../../framework/wpf/controls/control-authoring-overview.md). ## See also - [Tutorial: Create a new WPF app](../get-started/create-app-visual-studio.md) - [Migrate a WPF app to .NET Core](../migration/convert-project-from-net-framework.md) - [Overview of WPF windows](../windows/index.md) - [Data binding overview](../data/index.md) - [XAML overview](../xaml/index.md)