Thursday, August 19, 2010

Implementing Data Binding in Silverlight Applications

Implementing Data Binding in Silverlight Applications


An extremely useful feature of Silverlight is the ability to bind properties of Silverlight controls to data sources. When the values of the data sources change, the property values of the controls change as well.

Data binding can be implemented as one-time, one-way, or two-way. One-way data binding only changes the value of the control based on the data source. In two-way binding, if either the control value or the source value changes, the other is updated as well.

To implement data binding in XAML code, you first need to implement a Binding statement encased in {} brackets in place of the property value. The Binding statement uses the following syntax:

{Binding < value >, < options >}

The value setting in the Binding statement is the property of the data source that is used to fill in the value for the control property. You can also specify the following options in the Binding statement:

 Converter: Specifies the converter object that is called by the binding engine to modify the data as it is passed between the data source and target property

 ConverterCulture: Specifies the culture used by the converter to implement conversion of data

 ConverterParamter: Specifies a parameter that can be passed to the converter

 Mode: Specifies whether the data flow in the binding is one-way or two-way.

 Source: Specifies the source object for the binding

The Binding statement tells the control only what property of the data source the value is bound to. You also need to set the value of the DataContext property of the control to assign a specific object as the data source.


To implement a class as a data source, the class must implement the INotifyProperty Changed interface and have an event of type PropertyChangedEventHandler named PropertyChanged.


The DataContext property is inheritable by child elements in the Silverlight application. If you have several controls that are bound to the same data source, then you can group those controls in a parent container such as a Canvas or Grid and then set the DataContext property of the parent. The data context flows down to the children without the need to set the property for each child.

Eg:
 
< TextBox Height="24" HorizontalAlignment="Left" Margin="60,72,0,0" VerticalAlignment="Top" Width="150" Text="{Binding Name, Mode=TwoWay}" TextWrapping="Wrap" x:Name="txtName" / >
 
 
write the following code
 
public class Employee : INotifyPropertyChanged

{
string name;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyChange(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}

public string Name
{
set
{
name = value;
NotifyChange("Name");
}
get
{
return name;
}
}
}


-------------

public partial class Page : UserControl

{
Employee emp;


public Page()
{
InitializeComponent();


emp = new Employee{ Name = "Jobin John" };


LayoutRoot.DataContext = emp;


}
}
}


Hope its Helpfull for silverlight programmers.......

No comments: