views:

221

answers:

1

I'm just starting to work with Silverlight (no WPF experience either) and am having a difficult time finding a source that provides a full explanation of Data Binding. There is absolutely no lack of tutorials (starting with the ones on Silverlight.net or Scott Gu's blogs), but everything I have found is "by example".

Is there a resource that explains how data binding works in Silverlight, from a Fundamental/Conceptual perspective, and provides end-to-end coverage of data binding features?

The desire for a more fundamental source of information is driven by a number of questions that came up this afternoon in reviewing tutorials and writing sample apps, such as:

  • Why can't I bind the value of a slider like this?: Value="{Binding=Age, Mode=TwoWay}" where Age refers to an int property in the object data context I bind in code-behind (the Visual Studio error message is Expected '[]'.
  • How do I use the DataContext property in VS 2010? What's a Path, Relative Source, Static Source, ...?
+1  A: 

Silverlight's data binding engine is very similar to WPF's. A decent overview is here http://msdn.microsoft.com/en-us/library/ms752347.aspx.

To answer your specific questions:

  • You should be able to do that. I just created a little project in Blend to try this out, and it worked great. One bit of trickiness there is the data binding engine is actually internally converting between double and int for you (as Slider.Value is a double).

  • DataContext is used for setting the context for any binding expressions below that element in the tree. A path is relative to the DataContext by default. For example, if you set your DataContext on your root element to itself using DataContext="{Binding RelativeSource={RelativeSource Self}}", then you could add something like <TextBlock Text="{Binding Width}" />, to add a TextBlock which shows the width of the root element. Path is just an implicit argument, so you could do "{Binding Path=Width}", and it would do the same thing.

  • A Relative Source allows you to choose to not use the DataContext, but instead some other source, like yourself in {RelativeSource Self}.

Gary Linscott