Chapter 18. Data Binding

WPF data binding lets you bind a target to a data source so the target automatically displays the value in the data source. For example, this lets you:

  • Make a ListBox display an array of values defined in XAML code.

  • Make a ListBox display a list of objects created in code-behind.

  • Make a TreeView build a hierarchical display of objects created in code-behind.

  • Make TextBoxes, Labels, and other controls display additional detail about the currently selected item in a ListBox, ComboBox, or TreeView.

Additional WPF data-binding features let you sort, filter, and group data; let the user modify a control to update a data source; and validate changes to data.

This chapter provides an introduction to data binding in WPF. It explains how to use bindings to associate objects and how to use bindings to let a control use values supplied by other controls, XAML resources, and objects created in code-behind.

Binding Basics

Data bindings have these four basic pieces:

  • Target — The object that will use the result of the binding

  • Target Property — The target object's property that will use the result

  • Source — The object that provides a value for the target object to use

  • Path — A path that locates the value within the source object

As a trivial example, suppose you want to bind a Label control's Content property so that the Label displays whatever you type in a TextBox. If the Label control is named lblResult and the TextBox is named txtTypeHere, then you would need to create a binding ...

Get WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.