Tuesday, January 27, 2009

DataBinding in WPF using DependencyProperties

One main feature of WPF is the powerful and reasonably user friendly way to do data binding. WPF makes this easily available courtesy of dependency properties which can automatically manage their change tracking and two-way updating of bindings.
Typically, each binding has four components: a binding target object, a target property, a binding source, and a path to the value in the binding source to use. For example, if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.
The target property must be a dependency property. Most UIElement properties are dependency properties and most dependency properties, except read-only ones, support data binding by default.
Creating a Binding uses the Binding object, and each binding usually has four components: binding target, target property, binding source, and a path to the source value to use. The rest of this post discusses how to set up a binding using Dependency Properties.
Configuring a binding with the framework is simply a matter of telling the WPF dependency property system the details about the desired binding. You do this via the Binding markup extension class:

The target is usually a WPF UI element, though there is no requirement for it to be a visual element.
The data target is the destination for the bound data. You have to specify both the target object and target property. The target property must be a dependency property. Dependency properties are a core part of the WPF dependency property system. They are the enablers for many of the exciting WPF features like animation, styles, templates, and property inheritance. They also provide the glue for data binding.
There are a variety of WPF elements that can serve as data targets. Some of these elements are designed for showing single values. The TextBlock and Slider elements are prime examples of this type of element. ItemsControls are elements that show lists of data. WPF includes a number of these list-friendly controls including ComboBox, ListBox, and TreeView.

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. If you want to create DataBinding to a target object using Dependency Property, the source object should be of Dependency Object.
public class User : DependencyObject
{
public static DependencyProperty IdProperty =
DependencyProperty.Register("Id", typeof(int), typeof(User),
new PropertyMetadata(0, (x, y) => ((User)x).OnIdPropertyChanged((int)y.NewValue)));

public static DependencyProperty FirstNameProperty =
DependencyProperty.Register("FirstName", typeof(string), typeof(User),
new PropertyMetadata(string.Empty, (x, y) => ((User)x).OnFirstNamePropertyChanged((string)y.NewValue)));

public static DependencyProperty LastNameProperty =
DependencyProperty.Register("LastName", typeof(string), typeof(User),
new PropertyMetadata(string.Empty, (x, y) => ((User)x).OnLastNamePropertyChanged((string)y.NewValue)));

//More code
}

No comments: