Programming WPF: Building Windows UI with Windows Presentation Foundation

Building Windows UI with Windows Presentation Foundation discussions of the powerful new programming styles that WPF brings to Windows development.
Table of contents

WPF, previously known as "Avalon", was initially released as part of. WPF uses DirectX and attempts to provide a consistent programming model for building applications. These elements can then be linked and manipulated based on various events, user interactions, and data bindings. Microsoft Silverlight provided functionality that is mostly a subset of WPF to provide embedded web controls comparable to Adobe Flash. Graphics, including desktop items like windows, are rendered using Direct3D.

This allows the display of more complex graphics and custom themes, at the cost of GDI's wider range of support and uniform control theming. It allows Windows to offload some graphics tasks to the GPU. This reduces the workload on the computer's CPU. GPUs are optimized for parallel pixel computations. This tends to speed up screen refreshes at the cost of decreased compatibility in markets where GPUs are not necessarily as powerful, such as the netbook market.

It is part of the. WPF's emphasis on vector graphics allows most controls and elements to be scaled without loss in quality or pixelization , thus increasing accessibility. With the exception of Silverlight, Direct3D integration allows for streamlined 3D rendering.


  • Spiritual Wanderer?
  • Believe in Yourself (Mini Book) (Petites).
  • Lost & Found.
  • Progress in Colorectal Surgery;

In addition, interactive 2D content can be overlaid on 3D surfaces natively. XAML is designed as a more efficient method of developing application user interfaces [ citation needed ]. The specific advantage that XAML brings to WPF is that XAML is a completely declarative language, allowing the developer or designer to describe the behavior and integration of components without the use of procedural programming.

Although it is rare that an entire application will be built completely in XAML, the introduction of XAML allows application designers to more effectively contribute to the application development cycle. Using XAML to develop user interfaces also allows for separation of model and view, which is considered a good architectural principle. As in web development, both layouts and specific themes are well suited to markup, but XAML is not required for either.

Indeed, all elements of WPF may be coded in a. The XAML code can ultimately be compiled into a managed assembly in the same way all.

Reward Yourself

The architecture of WPF spans both managed code and native code components. However, the public API exposed is only available via managed code. While the majority of WPF is in managed code, the composition engine which renders the WPF applications is a native component. It interfaces directly with DirectX and provides basic support for 2D and 3D surfaces, timer-controlled manipulation of contents of a surface with a view to exposing animation constructs at a higher level, and compositing the individual elements of a WPF application into a final 3D "scene" that represents the UI of the application and renders it to the screen.

WPF exposes a property system for objects which inherit from DependencyObject , that is aware of the dependencies between the consumers of the property, and can trigger actions based on changes in properties. Properties can be either hard coded values or expressions, which are specific expressions that evaluate to a result.

In the initial release, however, the set of expressions supported is closed. WPF properties support change notifications , which invoke bound behaviors whenever some property of some element is changed. Custom behaviors can be used to propagate a property change notification across a set of WPF objects. A property that's implemented by a parent control for use by child controls is a WPF construct called an attached property.

Most applications are created to provide users with the means to view and edit data. After the data is accessed and loaded into an application's managed objects, the hard work for WPF applications begins. Essentially, this involves two things:. Copying the data from the managed objects into controls, where the data can be displayed and edited. To simplify application development, WPF provides a data binding engine to automatically perform these steps. The core unit of the data binding engine is the Binding class, whose job is to bind a control the binding target to a data object the binding source.

This relationship is illustrated by the following figure:. The next example demonstrates how to bind a TextBox to an instance of a custom Person object. The Person implementation is shown in the following code:. The following markup binds the TextBox to an instance of a custom Person object.

In this example, the Person class is instantiated in code-behind and is set as the data context for the DataBindingWindow. In markup, the Text property of the TextBox is bound to the Person. The WPF data binding engine provides additional support that includes validation, sorting, filtering, and grouping. Furthermore, data binding supports the use of data templates to create custom user interface for bound data when the user interface displayed by the standard WPF controls is not appropriate. For more information, see Data binding overview. WPF introduces an extensive, scalable, and flexible set of graphics features that have the following benefits:.

Resolution-independent and device-independent graphics.


  • WPF overview.
  • Windows Presentation Foundation - Wikipedia.
  • Andropathy.

Each device-independent pixel automatically scales to match the dots-per-inch dpi setting of the system it renders on. The WPF coordinate system is measured with double-precision floating-point numbers rather than single-precision. Transformations and opacity values are also expressed as double-precision. WPF also supports a wide color gamut scRGB and provides integrated support for managing inputs from different color spaces. Advanced graphics and animation support.

WPF simplifies graphics programming by managing animation scenes for you; there is no need to worry about scene processing, rendering loops, and bilinear interpolation. Additionally, WPF provides hit-testing support and full alpha-compositing support. WPF provides a library of common vector-drawn 2D shapes, such as the rectangles and ellipses that are shown in the following illustration:.

An interesting capability of shapes is that they are not just for display; shapes implement many of the features that you expect from controls, including keyboard and mouse input. The following example shows the MouseUp event of an Ellipse being handled. For more information, see Shapes and basic drawing in WPF overview. The 2D shapes provided by WPF cover the standard set of basic shapes.

Join Kobo & start eReading today

However, you may need to create custom shapes to facilitate the design of a customized user interface. For this purpose, WPF provides geometries. The following figure demonstrates the use of geometries to create a custom shape that can be drawn directly, used as a brush, or used to clip other shapes and controls. Path objects can be used to draw closed or open shapes, multiple shapes, and even curved shapes. Geometry objects can be used for clipping, hit-testing, and rendering 2D graphic data.

For more information, see Geometry overview. A subset of WPF 2D capabilities includes visual effects, such as gradients, bitmaps, drawings, painting with videos, rotation, scaling, and skewing. These are all achieved with brushes; the following figure shows some examples. For more information, see WPF brushes overview.

WPF also includes 3D rendering capabilities that integrate with 2-d graphics to allow the creation of more exciting and interesting user interfaces. For example, the following figure shows 2D images rendered onto 3D shapes. For more information, see 3D graphics overview. WPF animation support lets you make controls grow, shake, spin, and fade, to create interesting page transitions, and more.

You can animate most WPF classes, even custom classes. The following figure shows a simple animation in action. For more information, see Animation overview. One way to convey rich content is through the use of audiovisual media. WPF provides special support for images, video, and audio. Images are common to most applications, and WPF provides several ways to use them. The following figure shows a user interface with a list box that contains thumbnail images.

When a thumbnail is selected, the image is shown full-size. For more information, see Imaging overview. The MediaElement control is capable of playing both video and audio, and it is flexible enough to be the basis for a custom media player. The following XAML markup implements a media player. The window in the following figure shows the MediaElement control in action. For more information, see Graphics and multimedia. As a demonstration of text integration with graphics, the following figure shows the application of text decorations.

For more information, see Typography in Windows Presentation Foundation. Up to this point, you've seen the core WPF building blocks for developing applications. You use the application model to host and deliver application content, which consists mainly of controls. To simplify the arrangement of controls in a user interface, and to ensure the arrangement is maintained in the face of changes to window size and display settings, you use the WPF layout system.

Because most applications let users interact with data, you use data binding to reduce the work of integrating your user interface with data. To enhance the visual appearance of your application, you use the comprehensive range of graphics, animation, and media support provided by WPF. Often, though, the basics are not enough for creating and managing a truly distinct and visually stunning user experience.

The standard WPF controls might not integrate with the desired appearance of your application. Data might not be displayed in the most effective way. Your application's overall user experience may not be suited to the default look and feel of Windows themes. In many ways, a presentation technology needs visual extensibility as much as any other type of extensibility.

For this reason, WPF provides a variety of mechanisms for creating unique user experiences, including a rich content model for controls, triggers, control and data templates, styles, user interface resources, and themes and skins. NET Framework 4 or later. For more information, see How to: Target a Version of the. You also use this file to specify the UI that automatically shows when the application starts; in this case, MainWindow. 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. 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. For more information, see Navigation overview. Change the following properties on the NavigationWindow element:. Set the Title property to " ExpenseIt ".

Set the Width property to pixels. Set the Height property to pixels. Remove the Grid elements between the NavigationWindow tags. This file is a code-behind file that contains code to handle the events declared in MainWindow.

Create a WPF application in Visual Studio | Microsoft Docs

This file contains a partial class for the window defined in XAML. 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. Enter the name ExpenseItHome , and then select Add. This page is the first page that's displayed when the application is launched. It will show a list of people to select from, to show an expense report for.

Set the Title to " ExpenseIt - Home ". You can also set the Source property in the Miscellaneous category of the Properties window. Enter the name ExpenseReportPage , and then select Add.

What is Kobo Super Points?

This page will show the expense report for the person that is selected on the ExpenseItHome page. 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. Add an image named watermark. You can create your own image, copy the file from the sample code, or get it here. In the Add Existing Item dialog, browse to the image file you want to use, and then select Add. To build and run the application, press F5 or select Start Debugging from the Debug menu.

The following illustration shows the application with the NavigationWindow buttons:. Layout provides an ordered way to place UI elements, and also manages the size and position of those elements when a UI is resized. You typically create a layout with one of the following layout controls:.

Each of these layout controls supports a special type of layout for its child elements. ExpenseIt pages can be resized, and each page has elements that are arranged horizontally and vertically alongside other elements. Consequently, the Grid is the ideal layout element for the application. 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 pixel margin by adding column and row definitions to the Grid in ExpenseItHome. Set the Margin property on the Grid element to "10,0,10,10", which corresponds to left, top, right and bottom margins:.

You can also set the Margin values in the Properties window, under the Layout category:.