This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Data Binding

The eighty-ninth part of the Windows Presentation Foundation Fundamentals tutorial takes a break from looking at the WPF controls to consider data binding. This allows properties to be declaratively linked to information for automatic updating.

Data Conversion

Data binding often includes data conversion. For example, you can bind a TextBox's Text property to an integer or floating-point value. In this case the binding will automatically convert between string and numeric data with no additional coding.

Sometimes the conversion is more complex. For example, you might decide to include a Boolean property in your data object and link this to the Visibility property of a control, allowing you to show or hide the control according to the flag's value. However, Visibility is not a Boolean property; controls can be visible, hidden or collapsed. In this situation you might use a standard value converter, provided by the .NET framework, to convert between the two types.

For more complex scenarios still, you can create your own value converters that convert back and forth between two types of data. These are coded using a language such as C# and referenced in the XAML using binding expression options.


Data bindings can include formatting information, similar to that used when converting numeric data to formatted strings. For example, when displaying a number of financial transactions in a data grid, you might want to format the transaction amounts with a currency symbol and a fixed number of decimal places.

Data Templates

WPF allows you to create rich user interfaces. One of the ways to achieve this is through data templates, used with data binding. A template is created from XAML and defines the way in which a specific piece of information will be displayed. For example, you might use a data template to change the appearance of items in a ListBox. Instead of showing a simple list of strings, you might decide to show a combination of a string and an icon, with the icon based upon one or more properties of the binding source.


When you have complete control over the way in which data is transferred between the user interface and the business logic, you generally have to implement your own data validation. This ensures that the user cannot enter incorrect information.

The data binding system of WPF includes a validation system. Each binding can have one or more validation rules associated with it. WPF includes some prebuilt data validation rules, such as the ability to check that there was no exception during binding, as might happen when the user enters non-numeric characters into a TextBox that is bound to an integer value. You can also develop your own, reusable validation rules.

When the rules are not met, validation fails. You can fully control the result of a validation failure but WPF helps with options for automatically displaying elements or changing the template of the controls that are in error, highlighting the problem. For example, a TextBox's colour could be changed or an icon placed alongside it to show that the current value is not valid.

What's Next?

Over the next few articles we'll look at how you can use all of the above features, using WPF controls that we have already examined during the tutorial. We'll then return to look at the data display controls.

19 August 2014