Shawn Wildermuth's Rants and Raves

Thanks for visiting my blog! See more about me here: About Me

Data Binding Changes in Silverlight 4
Data Binding Changes in Silverlight 4
November 18, 2009

As many of you don’t know, I was previously known as “The ADO Guy” so of course my first jump into the new Silverlight 4 bits was to play with the new Data Binding changes. The improvements aren’t dramatic but they do fill several key holes that existed in the earlier versions.  Let’s take these changes one at a time.

DependencyObject Binding

Prior to Silverlight 4, in order to support data binding, the object had to derive from the FrameworkElement class which left out some key objects including Transformations.  Now data binding works on any object that derives from DependencyObject (which is most of the Silverlight 4 framework). You can see this in the example below.  (Note, you can also see the CompositeTransform which lets you set transformations of all four types in a single transform):

<Grid x:Name="LayoutRoot"
  <StackPanel Width="400"
        ScaleX="{Binding Value,ElementName=stretcher}"
        ScaleY="{Binding Value,ElementName=stretcher}" />
    <ListBox ItemsSource="{Binding}"
  <Slider Minimum=".5"
          Value="1" VerticalAlignment="Top" />

By adding the ability to use data bindings on DependencyObject derived objects, Silverlight 4 opens up using UI interactions through XAML.

String Formatting

Additionally, Silverlight 4 adds the ability to do string formatting directly in the data binding. Previously you would have to use a Converter to format data during binding. This greatly simplifies straightforward formatting of data into strings like dates and money values. It even uses the format to perform parsing for two-way data binding. To use it, simply use the markup extension “StringFormat” and the format (short or long form) as shown below:

<TextBox Text="{Binding ReleaseDate, StringFormat='MMM dd, yyyy', 
                        Mode=TwoWay}" />
<TextBlock Text="{Binding Price, StringFormat='c'}" />

Null and Fallback Values

Data binding has also added the ability to specify what to show in certain cases. These cases include when the value is NULL and when the value fails to load. There are two markup extensions that support this. First the TargetNull markup extension lets you specify what to show when the bound value is null.  Secondly, the FallbackValue is used in the same way but is shown when the value cannot be loaded through data binding (normally when the DataContext is null or fails to find the property on the bound object).  Here is an example of how that works:

<TextBlock Text="{Binding Developer, TargetNullValue='(None)'}" />
<TextBlock Text="{Binding Publisher, FallbackValue='(Nope)'}" />

CollectionViewSource Changes

For all of you who are doing grouping (using the DataGrid or similiar controls), good news. The CollectionViewSource now supports data binding to the GroupDescriptions so you can more easily describe your grouping in XAML.

Error Propogation

The Silverlight 3 validation stack was a good start, but still left a bit to be desired. Following the request from the community, controls will now use the IDataErrorInfo interface to get error information on your bound objects if it is available. To make this facility even better, they’ve introduced INotifyDataErrorInfo interface so that live bindings can efficiently know about changes to the IDataErrorInfo interface’s data.

Overall I am happy with the set of data binding functionality.  Enjoy playing with Silverlight 4 and let me know what you think of these changes!