- Next, create a new Silverlight 4 application and add the service reference to it.
- Create a Grid and then use the service reference to bind data to the Silverlight Grid.
- Lastly, build and execute the project
That's it!
Data-binding Improvements in Silverlight 4
Before Silverlight 4, to bind data, you were restricted to have the object (that would be used to bind data) derive from the FrameworkElement class. With Silverlight 4, you can now bind data using any object that is inhertited from DependencyObject.
Silverlight 4 introduces some other interesting new improvements in DataBinding. These include:IDataErrorInfo support, string formatting support, TargetNullValue, FallbackValue support, etc.
The following code snippet shows how String formatting can be used while binding data in Silverlight 4 controls:
<TextBox Text="{Binding DateOfBirth, StringFormat='MMM dd, yyyy',
Mode=TwoWay}" />
<TextBlock Text="{Binding Salary, StringFormat='c'}" />
You can use TargetNull value while data binding to Silverlight 4 controls if the element can contain a null value. You can use FallbackValue if no data is available to bind to the data control. The following code snippet shows how TargetNull and FallbackValue can be used:
<TextBlock Text="{Binding ZipCode, TargetNullValue='(None)'}" />
<TextBlock Text="{Binding Country, FallbackValue='(Nope)'}" />
Suggested Reading
Here are a few links to that provide further references on this topic:
http://www.microsoft.com/silverlight/
http://www.silverlight.net/getstarted/silverlight-4/
http://www.silverlight.net/learn/tutorials/silverlight-4/using-wcf-ria-services/
http://msdn.microsoft.com/en-us/library/ee354381.aspx
http://msdn.microsoft.com/en-us/library/ee621313.aspx
http://wildermuth.com/2009/11/18/Data_Binding_Changes_in_Silverlight_4
http://msdn.microsoft.com/en-us/data/bb931106
http://msdn.microsoft.com/en-us/data/aa937697
Summary
Windows Communication Foundation is a Microsoft framework that can be used to develop scalable, service oriented, REST-based services. There have been quite a few enhancements in WCF 4.0 that has eliminated the pain of specifying tedious configuration details to host services. In this article we have discussed the basics of WCF and Silverlight and how we can use WCF data services to bind data to Silverlight controls. Happy reading!