WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of .NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.
WPF was introduced as a part of .NET Framework 3.0 as the Windows library to build Windows client apps and the next generation of Windows Forms. The current version of WPF is 4.5.
WPF is the engine that is responsible for creating, displaying, and manipulating user-interfaces, documents, images, movies, and media in Windows 7 and later Windows operating systems. WPF is a set of libraries that have all functionality you need to build, run, execute, and manage Windows client applications.
WPF and XAML
XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next-generation managed applications. XAML is used to build user interfaces for Windows and Mobile applications that use Windows Presentation Foundation (WPF), UWP, and Xamarin Forms.
The purpose of XAML is simple, to create user interfaces using a markup language that looks like XML. Most of the time, you will be using a designer to create your XAML but you’re free to directly manipulate XAML by hand.
XAML uses the XML format for elements and attributes. Each element in XAML represents an object which is an instance of a type. The scope of a type (class, enumeration etc.) is defined a namespace that physically resides in an assembly (DLL) of the .NET Framework library.
Similar to XML, a XAML element syntax always starts with an open angle bracket (<) and ends with a close angle bracket (>). Each element tag also has a start tag and an end tag. For example, a Button object is represented by the <Button> object element. The following code snippet creates a Button object element.
<Button></Button>
Alternatively, you can use a self-closing format to close the bracket.
<Button />
WPF and C#
While XAML is used to build user interfaces in WPF, C# is used as the code-behind languages in WPF. While Windows and their controls are created in XAML at design-time, they can also be created at runtime using C# language.
C# is also used to write all events programming and business logic. All actions, events, and rendering is done in the code behind C# code.
WPF Resources
WPF supports two types of resources, static and dynamic.
Static Resources: A Static Resource will be resolved and assigned to the property during the loading of the XAML that occurs before the application is actually run. It will only be assigned once and any changes to the resource dictionary is ignored.
Dynamic Resources: A Dynamic Resource assigns an Expression object to the property during loading but does not actually lookup the resource until runtime when the Expression object is asked for the value. This defers looking up the resource until it is needed at runtime. A good example would be a forward reference to a resource defined later on in the XAML. Another example is a resource that will not even exist until runtime. It will update the target if the source's resource dictionary is changed.
WPF Styles
Style is a way to group similar properties in a single Style object and apply to multiple objects. The Style element in XAML represents a style. A Style is usually added to the resources of a FrameworkElement. The x:Key is the unique key identifier of the style.
Templates in WPF
Templates are an integral part of user interface design in WPF. This article explains templates, their types and how to use them in Windows applications.
WPF has the following three types of templates, Control Template, Items Panel Template, and Data Template.
Data Binding in WPF
Binding allows you to link a source object to some control. There are the following two types of bindings:
- One-Way Binding: Binds a source to the interface.
- Two-Way Binding: Binds a source to the interface and back to the source.
INotifyPropertyChanged interface allows sources to interact with the interface and update it as the values change.
- To bind an object or a list to an element you must set the DataContext property.
- It is possible to bind an object or a list of objects and you can bind one element to another.
- To customize how bound data will be displayed you can set the DataTemplate from a control.
- It is possible to set Data Converters to convert the source type to another type.
Triggers in WPF
Triggers are used to perform certain actions when a specified condition is fulfilled. Triggers are used to create visual effects on controls and framework elements. Triggers are parts of styles and are always defined inside a style.
Types of Triggers
Basically, there are 3 types of triggers, they are:
- Property Trigger
- Data Trigger
- Event Trigger
Media in WPF
WPF has two classes to work with audio, video and video with audio - MediaElement and MediaPlayer. The MediaElement is a part of XAML UIElement and is supported by both XAML and WPF code behind but MediaPlayer is available in WPF code behind only.
Charting in WPF
WPF supports common charts including line, bar, curve and others.
WPF Controls
WPF has a set of rich UI controls. These controls support visual actions such as drag and drop, setting properties and events, data binding, and setting up resources and templates.
Here are hundreds of WPF controls and code samples:
Learn WPF
WPF DatePicker Control
A DatePicker control is used to create a visual DatePicker that let user to pick a date and fire an event on the selection of the date. This article demonstrates how to create and use a DatePicker control in WPF with the help of XAML and C#.
WPF DockPanel
A Dock Panel is used to dock child elements in the left, right, top, and bottom positions of the relative elements. The position of child elements is determined by the Dock property of the respective child elements and the relative order of those child elements. The default value of Dock property is left. The Dock property is of type Dock enumeration that has Left, Right, Top, and Bottom values.
WPF ListBox
The XAML ListBox element represents a ListBox control.
1. <ListBox></ListBox>
The Width and Height properties represent the width and the height of a ListBox. The Name property represents the name of the control, which is a unique identifier of a control. The Margin property tells the location of a ListBox on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.
WPF ComboBox
A ComboBox control is an items control that works as a ListBox control but only one item from the collection is visible at a time and clicking on the ComboBox makes the collection visible and allows users to pick an item from the collection. Unlike a ListBox control, a ComboBox does not have multiple item selection. A ComboBox control is a combination of three controls - A Button, a Popup, and a TextBox. The Button control is used to show or hide available items and Popup control displays items and lets user select one item from the list. The TextBox control then displays the selected item.
WPF MessageBox
The MessageBox class in WPF represents a modal message box dialog, which is defined in the System.Windows namespace. The Show static method of the MessageBox is the only method that is used to display a message box. The Show method returns a MessageBoxResult enumeration that has the values None, OK, Cancel, Yes, and No. We can use MessageBoxResult to determine what button was clicked on a MessageBox and take an appropriate action.
WPF DataGrid
DataGrid element represents WPF DataGrid control in XAML.
1. <DataGrid />
When you drag and drop a DataGrid control from Toolbox to your designer, position the control, this action adds the following code to XA
The Width and Height properties represent the width and the height of a DataGrid. The Name property represents the name of the control, which is a unique identifier of a control. The Margin property sets the margin of placement of DataGrid on the window.
WPF ProgressBar
The ProgressBar tag in XAML represents a WPF ProgressBar control.
1. <ProgressBar></ProgressBar>
The Width and Height properties represent the width and the height of a ProgressBar. The Name property represents the name of the control, which is a unique identifier of a control. The Margin property tells the location of a ProgressBar on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.
WPF TreeView
A TreeView represents data in a hierarchical view in a parent child relationship where a parent node can be expanded or collapsed. The left side bar of Windows Explorer is an example of a TreeView.
In this tutorial, we will create a WPF application that will add and delete a TreeView items dynamically. The UI looks like the following where anything entered in the TextBox will be added to the TreeView as a child node.
WPF Interview Questions
Are you going for a WPF interview? Here is a list if top WPF interview questions and their answers.