Home > View Post

First look at Binding in Silverlight 2.0 Beta 1

At last, I've managed to spend a bit of time experimenting with Silverlight 2.0 Beta 1 and I instantly headed to investigate databinding (my favourite feature in WPF) to see what had made it to the Silverlight version and what hadn't.

A quick look at Reflector shows that the Binding object has far fewer bits and pieces than it's WPF counterpart.

Here's WPF's Binding:

WPF Binding in Reflector

and here's Silverlight 2.0's (beta 1):

Silverlight Binding in Reflector

It's clear that there's a lot less there, but what is missing? Here are some of the more notable omissions.

No Path Property

What!? No Path property!!! Calm down, the Binding in Silverlight 2.0 does support the concept of a Path (thank heavens!) but it doesn't expose a property of Path, it's always passed in as the constructor which means that this would error(With a nasty "Object Reference Not Set To Instance Of An Object" exception too - not very helpful but I suspect this will improve come RTW).

<TextBox Text="{Binding Path=MyProperty, Source={StaticResource someResource}}" />

Instead, the Path is specified as the default property (without a Name=) like so:

<TextBox Text="{Binding MyProperty, Source={StaticResource someResource}}" />


Limited Binding Mode Options

The good news is, contrary to what I had been told previously, Silverlight 2.0 does support TwoWay databinding. This was a huge relief to me! However, Mode options available to Silverlight 2.0 are reduced with only OneTime, OneWay and TwoWay (hoorah!) being available. The only option missing is WPF's OneWayToSource. I can live with that.

No UpdateSourceTrigger property

WPF allows the TwoWay and OneWayToSource binding modes to specify what causes the 'write back' to the source using the UpdateSourceTrigger which can be set to LostFocus, PropertyChanged and Explicit. This isn't available on Silverlight's binding object but the behaviour I've observed (with a TextBox) is LostFocus. However, as always, a Slider updates the source in real time.

No RelativeSource property

Not a feature I've used much in WPF (having favoured using the ElementName in most scenarios) but I think quite a few people will mourn the loss of the RelativeSource capability in Silverlight.

No ElementName property

I think this is the most notable omission yet and several customers I've spoken too feel the same about this. The ability to directly bind one element to another is sadly missing in Silverlight 2.0 :(

The good news is that there is a workaround but you'll need to write a little code (but only a little, and depending on your approach it could be very reusable). The workaround will be the subject of my next post so stay tuned.

No XPath property

So binding directly to Xml isn't supported (but of course you could use an XmlReader or XLinq to churn your Xml into CLR objects ready for binding).

Tags: Silverlight

Josh Post By Josh Twist
12:58 AM
12 Mar 2008

» Next Post: Workaround for missing ElementName in Silverlight 2.0 Binding
« Previous Post: Aston Martin at MIX 08

Comments are closed for this post.

Posted by David Cater @ 13 Mar 2008 2:06 PM
Have you made any attempt to bind to attached properites? I wasn't able to get that to work. For example, given a TextBox set as the DataContext, this binding works:

<TextBlock Text="{Binding Text}" />

But this binding does not:

<TextBlock Text="{Binding (Canvas.Top)}" />

Thanks for the helpful tips!


© 2005 - 2022 Josh Twist - All Rights Reserved.