Blog
Windows Presentation Foundation Using Visual C# 2010: Introducing Binding
Excerpt by Ken Getz and Robert Green | February 21, 2014
As you write applications, you often need to update the value of one element with information from another element. Often, you need to display information coming from a collection of objects in a list, or combo box. Or, perhaps you need to work with data coming from a data source, such as a database. In all these cases, the simplest solution is to bind data from a data source to a target element; in other words, you need binding capabilities. Data binding makes it simple for applications to display and interact with data. You use a connection, or binding, between the data source and the target; this binding object allows data to flow between the source and the target objects. Once you have created a binding, and you change data in the data source, the changes appear in the target. If you have enabled two-way data binding, you can also make changes in the target and have those changes appear back in the original data source. When might you use binding? You might use binding to:
- Display the number of characters in a text box, in a TextBlock control.
- Display the value of a Slider control in a TextBlock control.
- Display a list of customers in a ListBox control.
- Display a customer's photo in an Image control.
- Display and edit a customer's name in a TextBox control.
Obviously, there are infinite reasons to use binding, and XAML makes it easy.
Connecting Sources and Targets
Every time you use binding, you must supply a source for the data, and a target (normally, a dependency property of some user interface element). Figure 1 demonstrates the basic concepts of binding in XAML. As you can see, the binding source can be any property of any CLR object. The binding object connects this source to the binding target, which must be a dependency property of some object that inherits from FrameworkElement (all the standard controls inherit from this base class, and just about every property of these controls is a DependencyProperty object.)
Figure 1. The Binding object connects the binding source and target.
Although you can generally work blissfully unaware of this fact, when you use a binding in XAML you're actually creating an instance of the Binding class, setting various properties. XAML provides a markup extension that hides this fact, as you'll see, but all the same, you're working with a Binding class instance.
TIP: If you want to create a binding in code, you'll always need to create an instance of the Binding class, and set its various properties to match the properties you would otherwise set in XAML markup.
The flow shown in Figure 1 indicates that binding can work in multiple directions. By setting the Mode property of the Binding instance, you can indicate whether you want the data to move in one direction whenever changes are made to the source, in one direction only once, in two directions, or not at all.
Value Converters
The Value Converter block in Figure 1 represents an instance of a class that implements the System.Windows.Data.IValueConverter interface. You'll often need to use a value converter, especially if you want to do all of your binding declaratively, in the XAML markup. Imagine scenarios like these, in which value converters become necessary:
- You want to select a customer from a ListBox, and display the combined FirstName and LastName fields in a TextBlock.
- You want to select a number value, and use that value to set the BorderThickness of a Border control. You can't simply bind the number to the property, because the BorderThickness property of a Border is a Thickness object.
There are, obviously, infinite reasons to use a value converter, but unless you perform data binding declaratively (in the XAML markup, that is) there's no reason to use one. But if you would like to simplify your applications and reduce the amount of code you must maintain, using declarative binding is a truly useful tool, and it requires value converters in order to be fully useful.
This post is an excerpt from the online courseware Presentation Foundation Using Visual C# 2010 course written by expert Ken Getz and Robert Green.
Ken Getz
Robert Green
This course excerpt was originally posted February 21, 2014 from the online courseware Windows Presentation Foundation Using Visual C# 2010 by Ken Getz and Robert Green