Data Binding Changes in Silverlight 4

Silverlight LogoAs 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"
      Background="White">
 
  <StackPanel Width="400"
              Height="300">
    <StackPanel.RenderTransform>
      <CompositeTransform 
        ScaleX="{Binding Value,ElementName=stretcher}"
        ScaleY="{Binding Value,ElementName=stretcher}" />
    </StackPanel.RenderTransform>
    <ListBox ItemsSource="{Binding}"
             Width="400"
             Height="300">
    </ListBox>
  </StackPanel>
  <Slider Minimum=".5"
          Maximum="4"
          x:Name="stretcher"
          Value="1" VerticalAlignment="Top" />
</Grid>

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!

 

 

Comments

Gravatar

Michael Sync Thursday, November 19, 2009

Yeah. I love those changes..

Gravatar

Alexey Zakharov Friday, November 20, 2009

I hope that MS would release more binding features. Styles values are still not bindable. An still no x:Type x:Static and other WPF mark up extensions. =(

Gravatar

Bill Reiss Sunday, November 22, 2009

Can you data bind a behavior? I haven't tried that yet but that would open up a lot of new possibilities for them.

Gravatar

Ben Dewey Sunday, November 22, 2009

I like FallbackValue for quick DesignTime support, what are you thoughts on this?

Gravatar

uggmax Saturday, December 5, 2009


Many women wear <a href="http://www.uggmax.com/">cardy ugg boots</a> with jeans-another look suitable for cold weather.Choose skinny pants that can be tucked into the <a href="http://www.uggmax.com/">Uggs</a> without creating fabric bulges right above the <a href="http://www.uggmax.com/">ugg boot</a>.You can also wear <a href="http://www.uggmax.com/">cheap ugg classic cardy</a> in the spring and early fall, particularly at nighttime when the temperature drops.<a href="http://www.uggmax.com/">www.uggmax.com</a>

Gravatar

Victor Saturday, February 20, 2010

Really... this did not worked for me...

First I saw the implementation of this for a GradientStop Color Property in this article: http://blogs.msdn.com/coding4fun/archive/2006/11/30/1178235.aspx

Then I was told that this was just for WPF and for SL I should wait for version 4.0...

Well... I downloaded the VS.Net Beta 2 and could not use Binding with a Converter for a GradientStop's Color property...

Is this because of beta status and should work in the final version of the product or there's something about using it for a GradientStop?

Tanks,
Victor

Gravatar

cpx Monday, April 12, 2010

FINALLY! The lack of binding to DependencyObject has almost been a showstopper for a project I'm working in. Nice to see it finally included.

Gravatar

Mike Graham Saturday, May 22, 2010

Nice post... Have you looked at the string indexer improvement? Here is a link I was just looking at: http://www.scip.be/index.php?Page=ArticlesNET32#OtherImprovements

This supports binding to dictionary elements - and is something VERY useful for connecting to run-time defined data :)

I'm a fan of IEnumerable<T>, but for systems that are driven by configuration and connect to data sources based on configuration data, you don't know T at design / compile time.

Without string indexer binding, I've been resorting to run-time generation of T, which is complex and involves emitting IL.

Gravatar

Mike Graham Saturday, May 22, 2010

Nice post... Have you looked at the string indexer improvement? Here is a link I was just looking at: http://www.scip.be/index.php?Page=ArticlesNET32#OtherImprovements

This supports binding to dictionary elements - and is something VERY useful for connecting to run-time defined data :)

I'm a fan of IEnumerable<T>, but for systems that are driven by configuration and connect to data sources based on configuration data, you don't know T at design / compile time.

Without string indexer binding, I've been resorting to run-time generation of T, which is complex and involves emitting IL.

Gravatar

Ray Akkanson Thursday, July 8, 2010

Do you have any samples on INotifyDataErrorInfo usage?

Thanks
Ray Akkanson

Gravatar

Ehsan Yazdani Rad Saturday, December 4, 2010

Thanks Shawn, It was useful to me.

Gravatar

Tom Mallard Thursday, January 6, 2011

MikeG, irrc there's an anonymous type available for c#v4, f# has it so you don't need to know the type at design-time, <_> is used for this, or 'a.

Working on a f# lib for dynamic binding raised by INotifyPropertyChanged() in SilverLight, from blog: http://www.sharpfellows.com/post/static-reflection-inotifypropertychanged-2.aspx & ideas from System.Reactive ....


Leave a Comment

*
*
*