In the previous article I demonstrated how to create a simple Silverlight application using Microsoft Visual Studio 2010. In building business applications in Silverlight Data Binding is used very much. In this article I will try to explain some basics of Data Binding.
What is Data Binding?
Data binding is used to establish a connection between the properties of two objects. In this relationship, one object is referred to as the Source and one object is referred to as the Target. In the most typical scenario, source object is the object that contains data and target object is a control that displays that data.
Target: The target can be any DependencyProperty of a FrameworkElement.
Source: The source object that contains the data that flows between the source and target. The source can be any CLR object – like List, Collection…, including the target element itself or other UI elements. If the target is in a data template, the source can be the UI element to which the template is applied.
Direction: Each binding has a Mode property, which determines how and when the data flows. Silverlight enables three types of bindings:
Value Converter: The optional value converter that applies to the data as it is passed. The value converter is a class that implements IValueConverter.
What is Dependency Property?
As mentioned previously for Data binding a target property must be a dependency property. Dependency Property is like any other property but it can hold a default value, with built in mechanism for property value validation and automatic notification for changes in property value.
Dependency Properties are data-bound properties whose value is determined at runtime. The purpose of dependency properties is to provide a way to determine the value of a property based on inputs including user preferences, resources, styles, or data binding. Dependency Property follows an order of precedence to determine its value. If a Dependency Property is bound and in code the property is set to a value explicitly, the binding is removed.
What is DataContext?
The DataContext refers to a source of data that can be bound to a target. The DataContext often is set to an instance of an entity. DataContext can be bound to any controls that have access to it. It can be used to bind all controls within a container control to a single data source. This is useful when there are several controls that all use the same binding source. So DataContext is a concept that allows elements to inherit information from their parent elements about the data source that is used for binding, as well as other characteristics of the binding, such as the path.
Binding in XAML
In XAML, the data binding works through a specific markup extension syntax. A basic example of the usage of these extensions is shown below:
<TextBox Height="23" Name="txtItemNumber" Width="120" Text="{Binding ItemNumber}" />
There are a handful of binding properties that can be set with the XAML binding extensions in Silverlight. All of these properties are optional. These properties are:
- Converter
- ConverterCulture
- ConverterParameter
- Mode
- Source
- Path
- NotifyOnValidationError
- ValidatesOnExceptions
The Path can be the name of a property that exists on the source object.
The Mode indicates the binding mode that specifies how the binding will operate. Valid values for the Mode property are OneTime, OneWay and TwoWay.
- OneTime bindings update the target with the source data when the binding is created.
- OneWay bindings update the target with the source data when the binding is created and anytime the data changes. This is the default mode.
- TwoWay bindings update both the target and the source when either changes. Alternately, you can disable automatic source updates and update the source only at times of your choosing.
An object reference can be set to the Source property. if the Source property is omitted the data source will defer to the DataContext. If the Source property is set the object reference will override the DataContext as the data source for this binding.
Let’s create a class for Item which has Item Number, Item Description and Quantity.
public class Item
{
public string ItemNumber { get; set;}
public string ItemDescription { get; set; }
public int Quantity { get; set; }
}
Lets’ add a Stackpanel on the page with three textbox for “ItemNumber”, “Item Description” and “Quantity”.
<StackPanel Height="100" HorizontalAlignment="Left"
Margin="344,407,0,0" Name="stackPanel1"
VerticalAlignment="Top" Width="308" >
<TextBox Height="23" Name="txtItemNumber" Width="120"
Text="{Binding ItemNumber}" />
<TextBox Height="23" Name="txtItemDescription" Width="120"
Text="{Binding ItemDescription}" />
<TextBox Height="23" Name="txtQuantity" Width="120"
Text="{Binding Path=Quantity}" />
</StackPanel>
Below code will bind the StackPanel with the item’s object.
Item c = new Item();
c.ItemDescription = "Item Name1";
c.ItemNumber = "1";
c.Quantity = 1;
stackPanel1.DataContext = c;
Binding in Code behind
Binding class is defined in System.Windows.Data. Below code will bind the “ItemNumber” property to the textbox’s text property.
Item c2 = new Item();
C2.ItemNumber = “Item Number2”;
Binding mybinding = new Binding("ItemNumber");
mybinding.Source = c2;
txtQuantity.SetBinding(TextBox.TextProperty, mybinding);