RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Flexible and Powerful Data Binding with WPF : Page 2

Microsoft has been building data binding frameworks for years, and each promises to solve our data binding woes forever. But we're still waiting for the perfect one. Could WPF data binding be the one we've been waiting for?

Data Binding Collaborators
There are three main collaborators in the WPF data binding model: the framework, the data target, and the data source. 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:

   <TextBlock Text='{Binding Path=StreetAddress}' />
This short XAML (pronounced "zammel") snippet is all you need to bind the Text property to the StreetAddress property.

Data Targets
You always set bindings are always set on a binding target, which is usually a WPF UI element—though it isn't required to be a visual element.

The data target is the destination for the bound data. You have to specify both the target object and a target property. The target property must be a dependency property. Dependency properties are a core part of the WPF dependency property system. They enable exciting WPF features such as animation, styles, templates, and property inheritance. They also provide the glue for data binding.

Any type can serve as a data target as long as it provides one or more "dependency properties." To find out which properties are eligible, here's a simple LINQ query that returns all the dependency properties in the TextBlock class.

   // C#:
   // using System.Windows.Controls
   var q = from member
   in typeof(TextBlock).GetFields
      (BindingFlags.Public | BindingFlags.Static |

   where member.FieldType ==  
   orderby member.Name
   select member.Name;
   // Examples
   // BackgroundProperty
   // CursorProperty
   ' Visual Basic:
   ' using System.Windows.Controls
   Dim q = From member In _ 
   GetType(TextBlock).GetFields(BindingFlags.Public _
   Or BindingFlags.Static _
   Or BindingFlags.FlattenHierarchy) _
   Where member.FieldType Is _ GetType(System.Windows.DependencyProperty) _
   Order By member.Name _
   Select member.Name
   ' Examples
   ' BackgroundProperty
   ' CursorProperty
Author's Note: If you run the "List of Dependency Properties" item in the included sample project, you'll see a detailed list of available dependency properties for each framework element.

A variety of WPF elements can serve as data targets. Some are designed to show single values, such as the TextBlock and Slider elements. Other elements, such as ItemsControls, show lists of data. WPF includes a number of these list-friendly controls including ComboBox, ListBox, and TreeView.

Data Sources
One splendid aspect of the WPF data binding engine is that it can bind to nearly any data source: XML data, ADO.NET classes, LINQ queries, WPF elements, and any type that implements IEnumerable or IList.

As discussed in the preceding section, every binding must have a binding target and target property. But the binding isn't complete until you also specify the data source and source property.

Sometimes you want to show a single value from a data source. It might be showing a font name in a label or an invoice date in a calendar control. Perhaps you'd like to bind the value of one control to the input of another. I'll call this "single property" binding though there is no such official name in WPF. Single property binding is easy to do in WPF. Here is an example showing how to bind the Value property of a Slider control to the FontSize property of a TextBlock:

   <Slider x:Name='sizeSlider'
     Value='25' />
   <TextBlock Text='Sample Text - abcde'
     '{Binding ElementName=sizeSlider, Path= Value}'/>
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. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property. A binding path can be a complex value, drilling into the member hierarchy, but in this case it's just the name of the property.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date