WPF Applications
WPF Applications
Getting Started
Introduction to WPF in Visual Studio
What's New in WPF Version 4.5
Walkthrough: My first WPF desktop application
WPF Walkthroughs
WPF Community Feedback
Getting Started (WPF)
4/9/2018 • 1 min to read • Edit Online
Windows Presentation Foundation (WPF ) is a UI framework that creates desktop client applications. The WPF
development platform supports a broad set of application development features, including an application model,
resources, controls, graphics, layout, data binding, documents, and security. It is a subset of the .NET Framework, so
if you have previously built applications with the .NET Framework using ASP.NET or Windows Forms, the
programming experience should be familiar. WPF uses the Extensible Application Markup Language (XAML ) to
provide a declarative model for application programming. This section has topics that introduce and help you get
started with WPF.
Controls
Are you a Windows Forms developer? Windows Forms Controls and Equivalent WPF Controls
See Also
Class Library
Application Development
.NET Framework Developer Center
Introduction to WPF in Visual Studio
4/9/2018 • 1 min to read • Edit Online
Windows Presentation Foundation (WPF ) in Visual Studio provides developers with a unified programming model
for building line-of-business desktop applications on Windows.
Create Desktop Applications with Windows Presentation Foundation
Designing XAML in Visual Studio and Blend for Visual Studio
Introduction to WPF
WPF in the .NET Framework
Get Visual Studio
What's New in WPF Version 4.5
4/9/2018 • 6 min to read • Edit Online
This topic contains information about new and enhanced features in Windows Presentation Foundation (WPF )
version 4.5.
This topic contains the following sections:
Ribbon control
Improved performance when displaying large sets of grouped data
New features for the VirtualizingPanel
Binding to static properties
Accessing collections on non-UI Threads
Synchronously and Asynchronously validating data
Automatically updating the source of a data binding
Binding to types that Implement ICustomTypeProvider
Retrieving data binding information from a binding expression
Checking for a valid DataContext object
Repositioning data as the data's values change (Live shaping)
Improved Support for Establishing a Weak Reference to an Event
New methods for the Dispatcher class
Markup Extensions for Events
Ribbon control
WPF 4.5 ships with a Ribbon control that hosts a Quick Access Toolbar, Application Menu, and tabs. For more
information, see the Ribbon Overview.
Note that in the first case, the class exposes a static event named PropertyName Changed that passes EventArgs to
the event handler. In the second case, the class exposes a static event named StaticPropertyChanged that passes
PropertyChangedEventArgs to the event handler. A class that implements the static property can choose to raise
property-change notifications using either method.
See Also
What's New in the .NET Framework
Walkthrough: My first WPF desktop application
4/17/2018 • 18 min to read • Edit Online
This article shows you how to develop a simple Windows Presentation Foundation (WPF ) application that includes
the elements that are common to most WPF applications: Extensible Application Markup Language (XAML )
markup, code-behind, application definitions, controls, layout, data binding, and styles.
This walkthrough includes the following steps:
Use XAML to design the appearance of the application's user interface (UI).
Write code to build the application's behavior.
Create an application definition to manage the application.
Add controls and create the layout to compose the application UI.
Create styles for a consistent appearance throughout an application's UI.
Bind the UI to data to both populate the UI from data and keep the data and UI synchronized.
By the end of the walkthrough, you'll have built a standalone Windows application that allows users to view
expense reports for selected people. The application is composed of several WPF pages that are hosted in a
browser-style window.
TIP
The sample code that is used to build this walkthrough is available for both Visual Basic and C# at Introduction to Building
WPF Applications.
Prerequisites
Visual Studio 2012 or later
For more information about installing the latest version of Visual Studio, see Install Visual Studio.
NOTE
This walkthrough uses the DataGrid control that is available in the .NET Framework 4 and later. Be sure that your
project targets the .NET Framework 4 or later. For more information, see How to: Target a Version of the .NET
Framework.
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
<Application x:Class="ExpenseIt.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
3. Open MainWindow.xaml.
This XAML file is the main window of your application and displays content created in pages. The Window
class defines the properties of a window, such as its title, size, or icon, and handles events, such as closing or
hiding.
4. Change the Window element to a NavigationWindow, as shown in the following XAML:
<NavigationWindow x:Class="ExpenseIt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
...
</NavigationWindow>
This app navigates to different content depending on the user input. This is why the main Window needs to
be changed to a NavigationWindow. NavigationWindow inherits all the properties of Window. The
NavigationWindow element in the XAML file creates an instance of the NavigationWindow class. For more
information, see Navigation overview.
5. Change the following properties on the NavigationWindow element:
Set the Title property to "ExpenseIt".
Set the Width property to 500 pixels.
Set the Height property to 350 pixels.
Remove the Grid elements between the NavigationWindow tags.
Your XAML should look like this in Visual Basic:
<NavigationWindow x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ExpenseIt" Height="350" Width="500">
</NavigationWindow>
<NavigationWindow x:Class="ExpenseIt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ExpenseIt" Height="350" Width="500">
</NavigationWindow>
namespace ExpenseIt
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : NavigationWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
Class MainWindow
End Class
TIP
You can toggle the code language of the sample code between C# and Visual Basic in the Language drop-down on
the upper right side of this article.
</Grid>
</Page>
Or this in C#:
<Page x:Class="ExpenseIt.ExpenseItHome"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="ExpenseIt - Home">
<Grid>
</Grid>
</Page>
4. Open MainWindow.xaml.
5. Set the Source property on the NavigationWindow to "ExpenseItHome.xaml".
This sets ExpenseItHome.xaml to be the first page opened when the application starts. Your XAML should
look like this in Visual Basic:
<NavigationWindow x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ExpenseIt" Height="350" Width="500" Source="ExpenseItHome.xaml">
</NavigationWindow>
Or this in C#:
<NavigationWindow x:Class="ExpenseIt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ExpenseIt" Height="350" Width="500" Source="ExpenseItHome.xaml">
</NavigationWindow>
TIP
You can also set the Source property in the Miscellaneous category of the Properties window.
6. Add another new WPF page to the project, and name it ExpenseReportPage.xaml::
a. In Solution Explorer, right-click on the ExpenseIt project node and choose Add > Page.
b. In the Add New Item dialog, the Page (WPF) template is already selected. Enter the name
ExpenseReportPage, and then select Add.
This page will show the expense report for the person that is selected on the ExpenseItHome page.
7. Open ExpenseReportPage.xaml.
8. Set the Title to "ExpenseIt - View Expense".
Your XAML should look like this in Visual Basic:
<Page x:Class="ExpenseReportPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="ExpenseIt - View Expense">
<Grid>
</Grid>
</Page>
Or this in C#:
<Page x:Class="ExpenseIt.ExpenseReportPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="ExpenseIt - View Expense">
<Grid>
</Grid>
</Page>
9. Open ExpenseItHome.xaml.vb and ExpenseReportPage.xaml.vb, or ExpenseItHome.xaml.cs and
ExpenseReportPage.xaml.cs.
When you create a new Page file, Visual Studio automatically creates a code-behind file. These code-behind
files handle the logic for responding to user input.
Your code should look like this for ExpenseItHome:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 ExpenseIt
{
/// <summary>
/// Interaction logic for ExpenseItHome.xaml
/// </summary>
public partial class ExpenseItHome : Page
{
public ExpenseItHome()
{
InitializeComponent();
}
}
}
Class ExpenseItHome
End Class
namespace ExpenseIt
{
/// <summary>
/// Interaction logic for ExpenseReportPage.xaml
/// </summary>
public partial class ExpenseReportPage : Page
{
public ExpenseReportPage()
{
InitializeComponent();
}
}
}
Class ExpenseReportPage
End Class
10. Add an image named watermark.png to the project. You can create your own image, copy the file from the
sample code, or get it here.
a. Right-click on the project node and select Add > Existing Item, or press Shift+Alt+A.
b. In the Add Existing Item dialog, browse to the image file you want to use, and then select Add.
TIP
For more information about Panel elements, see Panels overview. For more information about layout, see Layout.
In the section, you create a single-column table with three rows and a 10-pixel margin by adding column and row
definitions to the Grid in ExpenseItHome.xaml.
1. Open ExpenseItHome.xaml.
2. Set the Margin property on the Grid element to "10,0,10,10", which corresponds to left, top, right and
bottom margins:
<Grid Margin="10,0,10,10">
TIP
You can also set the Margin values in the Properties window, under the Layout category:
3. Add the following XAML between the Grid tags to create the row and column definitions:
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
The Height of two rows is set to Auto, which means that the rows are sized base on the content in the rows.
The default Height is Star sizing, which means that the row height is a weighted proportion of the available
space. For example if two rows each have a Height of "*", they each have a height that is half of the available
space.
Your Grid should now look like the following XAML:
<Grid Margin="10,0,10,10">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
</Grid>
Add controls
In this section, you'll update the home page UI to show a list of people that a user can select from to show the
expense report for. Controls are UI objects that allow users to interact with your application. For more information,
see Controls.
To create this UI, you'll add the following elements to ExpenseItHome.xaml:
ListBox (for the list of people).
Label (for the list header).
Button (to click to view the expense report for the person that is selected in the list).
Each control is placed in a row of the Grid by setting the Grid.Row attached property. For more information about
attached properties, see Attached Properties Overview.
1. Open ExpenseItHome.xaml.
2. Add the following XAML somewhere between the Grid tags:
<!-- People list -->
<Border Grid.Column="0" Grid.Row="0" Height="35" Padding="5" Background="#4E87D4">
<Label VerticalAlignment="Center" Foreground="White">Names</Label>
</Border>
<ListBox Name="peopleListBox" Grid.Column="0" Grid.Row="1">
<ListBoxItem>Mike</ListBoxItem>
<ListBoxItem>Lisa</ListBoxItem>
<ListBoxItem>John</ListBoxItem>
<ListBoxItem>Mary</ListBoxItem>
</ListBox>
TIP
You can also create the controls by dragging them from the Toolbox window onto the design window, and then
setting their properties in the Properties window.
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
4. Move the controls to the second column by setting the Grid.Column property to 1 in each of the three
controls (Border, ListBox, and Button).
5. Move each control down a row, by incrementing its Grid.Row value by 1.
The XAML for the three controls now looks like this:
6. Set the Background of the Grid to be the watermark.png image file, by adding the following XAML
somewhere between the <Grid> and <\/Grid> tags:
<Grid.Background>
<ImageBrush ImageSource="watermark.png"/>
</Grid.Background>
7. Before the Border element, add a Label with the content "View Expense Report". This is the title of the page.
End Sub
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
Style controls
The appearance of various elements is often the same for all elements of the same type in a UI. UI uses styles to
make appearances reusable across multiple elements. The reusability of styles helps to simplify XAML creation and
management. This section replaces the per-element attributes that were defined in previous steps with styles.
1. Open Application.xaml or App.xaml.
2. Add the following XAML between the Application.Resources tags:
<!-- Header text style -->
<Style x:Key="headerTextStyle">
<Setter Property="Label.VerticalAlignment" Value="Center"></Setter>
<Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
<Setter Property="Label.FontWeight" Value="Bold"></Setter>
<Setter Property="Label.FontSize" Value="18"></Setter>
<Setter Property="Label.Foreground" Value="#0066cc"></Setter>
</Style>
<Grid.Background>
<ImageBrush ImageSource="watermark.png" />
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
The properties such as VerticalAlignment and FontFamily that define the look of each control are removed
and replaced by applying the styles. For example, the headerTextStyle is applied to the "View Expense
Report" Label.
5. Open ExpenseReportPage.xaml.
6. Replace everything between the Grid elements with the following XAML:
<Grid.Background>
<ImageBrush ImageSource="watermark.png" />
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
</Grid.Resources>
The data is created as a Grid resource. Normally this would be loaded as a file, but for simplicity the data is
added inline.
3. Within the <Grid.Resources> element, add the following DataTemplate, which defines how to display the
data in the ListBox:
<Grid.Resources>
</Grid.Resources>
For more information about data templates, see Data templating overview.
4. Replace the existing ListBox with the following XAML:
This XAML binds the ItemsSource property of the ListBox to the data source and applies the data template
as the ItemTemplate.
End Class
}
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
' View Expense Report
Dim expenseReportPage As New ExpenseReportPage(Me.peopleListBox.SelectedItem)
Me.NavigationService.Navigate(expenseReportPage)
End Sub
3. After the opening Grid element, add the following data templates, which define how to display the expense
report data:
4. Apply the templates to the DataGrid columns that display the expense report data.
<DataGrid.Columns>
<DataGridTextColumn Header="ExpenseType" Binding="{Binding XPath=@ExpenseType}" />
<DataGridTextColumn Header="Amount" Binding="{Binding XPath=@ExpenseAmount}" />
</DataGrid.Columns>
</DataGrid>
NOTE
This sample demonstrates a specific feature of WPF and doesn't follow all best practices for things like security, localization,
and accessibility. For comprehensive coverage of WPF and the .NET Framework application development best practices, see
the following topics:
Accessibility
Security
WPF globalization and localization
WPF performance
Next steps
In this walkthrough you learned a number of techniques for creating a UI using Windows Presentation Foundation
(WPF ). You should now have a basic understanding of the building blocks of a data-bound, .NET Framework
application. For more information about the WPF architecture and programming models, see the following topics:
WPF architecture
XAML overview (WPF )
Dependency properties overview
Layout
For more information about creating applications, see the following topics:
Application development
Controls
Data binding overview
Graphics and multimedia
Documents in WPF
See also
Panels overview
Data templating overview
Build a WPF application
Styles and templates
WPF Walkthroughs
4/9/2018 • 1 min to read • Edit Online
Walkthroughs give step-by-step instructions for common scenarios. This makes them a good place to start
learning about the product or a particular feature area.
This topic contains links to Windows Presentation Foundation (WPF ) walkthroughs.
Walkthrough: Building a Simple WPF Application with the WPF Demonstrates how to build a simple WPF application with the
Designer WPF Designer.
Walkthrough: Constructing a Dynamic Layout Demonstrates creating a dynamic layout by using a Grid panel
control.
Walkthrough: Creating a Resizable Application by Using the Demonstrates how to create window layouts that are resizable
WPF Designer by the user at run time.
Walkthrough: Creating a Data Binding by Using the WPF Demonstrates how to use the WPF Designer to create data
Designer bindings that connect data to a control.
Walkthrough: Using a DesignInstance to Bind to Data in the Demonstrates how to use the WPF Designer to create data
Designer bindings at design time for a data context that is assigned at
run time.
WPF Walkthroughs
TITLE DESCRIPTION
Walkthrough: My first WPF desktop application Demonstrates creating a WPF application using many of the
common features of WPF including controls, layout and data
binding.
Create a Button by Using Microsoft Expression Blend Demonstrates the process of creating a customized button by
using Microsoft Expression Blend.
Walkthrough: Display Data from a SQL Server Database in a Demonstrates how to retrieve data from a SQL Server
DataGrid Control database and display that data in a DataGrid control.
Walkthrough: Hosting a Windows Forms Control in WPF Demonstrates how to host a Windows Forms
System.Windows.Forms.MaskedTextBox control in a WPF
application.
Walkthrough: Hosting a Windows Forms Composite Control in Demonstrates how to host a Windows Forms data-entry
WPF composite control in a WPF application.
Walkthrough: Hosting a WPF Composite Control in Windows Demonstrates how to host a WPF data-entry composite
Forms control in a Windows Forms application.
Walkthrough: Arranging Windows Forms Controls in WPF Demonstrates how to use WPF layout features to arrange
Windows Forms controls in a hybrid application.
Walkthrough: Binding to Data in Hybrid Applications Demonstrates how to use data binding in hybrid applications
that include both Windows Forms and WPF controls.
Related Sections
TITLE DESCRIPTION
Visual Studio Walkthroughs Gives a related list of walkthroughs for all areas of
programming in Visual Studio.
WPF community feedback
4/9/2018 • 4 min to read • Edit Online
Microsoft exposes a variety of community resources for you to learn about, discuss, and provide feedback on
Windows Presentation Foundation (WPF ). These resources include forums and the Visual Studio Developer
Community site. Each community resource offers a different set of benefits. These benefits are described here, as
are a set of best practices for using each to ensure the best response from the community at large and Microsoft in
particular.
NOTE
Don't use the feedback section located at the bottom of each page to send product feedback. These links are for
documentation feedback only.
Forums
The WPF forum is the primary community resource for discussing and resolving issues. Forums facilitate
discussion and problem resolution by offering a comprehensive set of supporting features that include:
Searching.
Discussion tracking.
Rich formatting for text and code.
Visual Studio integration.
Most Valued Professional (MVP ) and community involvement.
Monitoring to ensure posts are responded to in the quickest possible time.
Another option for you to ask questions to the community about WPF is Stack Overflow.
Forum best practices
Using the following best practices help to address issues posted to the WPF forum in the quickest possible time.
These practices are applicable to all forums.
Search existing posts
Some issues occur widely enough that others have faced them before you. Consequently, you can solve your
problem quickly, or you can add your input to an existing discussion.
Use meaningful titles
Concise, meaningful titles improve the discoverability of your posts. They also make it easier for other WPF forum
community members to determine if they can solve your problem.
Include appropriate content
Describe the issue and how you’ve tried to work through it. If possible, include supporting code snippets, or the
simplest possible sample that demonstrates your issue. All these details help to increase the chance your question
will be answered quickly.
See also
How to report a problem with Visual Studio 2017