Silverlight Data Binding vs. WPF


Data Binding in Silverlight:

  • Silverlight doesn't support binding to the ADO.NET DataSet classes, because Silverlight doesn't include any part of ADO.NET.
  • Silverlight doesn't include a CollectionView class for changing the way that a collection of objects is sorted and filtered.
  • Silverlight doesn't support grouped data.
  • Silverlight doesn't support the IDataErrorInfo interface.
  • Silverlight value converters can act on only one data property, not multiple ones.
  • Silverlight doesn't allow you to create selectors that dynamically choose the right style or template for bound data.
  • Silverlight doesn't include object providers for code-free data binding. (This feature was of limited use in WPF anyway, unless you needed a quick code-free way to bind XML data.)
  • Silverlight doesn't let you define multiple, prioritized bindings, which would let you display a temporary value while waiting for information that takes longer to retrieve.
  • Some of these limitations cut out specialized features that WPF developers rarely use.
Please ind the below code for Bind to a DataObject.

The best way to try out Silverlight's data-binding features is to create a simple data object. Then, you can use data-binding expressions to display the data from your data object without writing tedious data-display code.

A data object is a package of related information. Any class will work as a data object, provided it consists of public properties. (A data object can also have fields and private properties, but you can't extract the information these members contain through data-binding expressions.) Furthermore, if you want to the user to be able to modify a data object through data binding, its properties can't be read-only.

Here's a simple data object that encapsulates the information for a single product in a product catalog:

public class Product
{
     private string modelNumber;
     public string ModelNumber
     {
         get { return modelNumber; }
         set { modelNumber = value; }
     }
     private string modelName;
     public string ModelName
     {
         get { return modelName; }
         set { modelName = value; }
     }
     private double unitCost;
     public double UnitCost
     {
         get { return unitCost; }
         set { unitCost = value; }
     }
     private string description;
     public string Description
     {
         get { return description; }
         set { description = value; }
     }
     public Product(string modelNumber, string modelName,
     double unitCost, string description)
     {
         ModelNumber = modelNumber;
         ModelName = modelName;
         UnitCost = unitCost;
         Description = description;
     }
}

Storing a Data Object as a Resource

You have one other option for specifying a data object. You can define it as a resource in your XAML markup and then alter each binding expression by adding the Source property.

For example, you can create the Product object as a resource using markup like this:

<UserControl.Resources>
<local:Product x:Key="resourceProduct"
ModelNumber="AEFS100"
ModelName="Portable Defibrillator" UnitCost="77"
Description="Analyzes the electrical activity of a person's heart and applies
an electric shock if necessary.">
</local:Product>
</UserControl.Resources>

This markup assumes you've mapped the project namespace to the XML namespace prefix local. For example, if the project is named DataBinding, you need to add this attribute to the UserControl start tag:

xmlns:local="clr-namespace:DataBinding"

To use this object in a binding expression, you need to specify the Source property. To set the Source property, you use a StaticResource expression that uses the resource's key name:

<TextBox
Text="{Binding ModelNumber, Source={StaticResource resourceProduct} }">
</TextBox>

Unfortunately, you must specify the Source property in each data-binding expression. If you need to bind a significant number of elements to the same data object, it's easier to set the DataContext property of a container. In this situation, you can still use the StaticResource to set the DataContext property, which allows you to bind a group of nested elements to a single data object that's defined as a resource:

<Grid Name="gridProductDetails" DataContext="{StaticResource resourceProduct}">

Please find the source code for detail use Like Binding to Data Object, Binding to Collection Object , Data Conversion and DataTemplate.


Similar Articles