Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Dinesh's Blog
Browse by Tags · View All
brh 20
#SILVERLIGHT 19
#DOTNET 18
silverlight 18
#ASP.NET 15
Silverlight 4.0 9
bing 7
bing maps 6
wcf services 5
map 5

Archive · View All
August 2010 10
September 2010 3
July 2010 3
June 2010 3
October 2010 1

Data Binding in Silverlight

Sep 27 2010 7:03AM by Dinesh Sodani   

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.

IC210095

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:

  1. Converter
  2. ConverterCulture
  3. ConverterParameter
  4. Mode
  5. Source
  6. Path
  7. NotifyOnValidationError
  8. 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.

  1. OneTime bindings update the target with the source data when the binding is created.
  2. OneWay bindings update the target with the source data when the binding is created and anytime the data changes. This is the default mode.
  3. 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);

Tags: silverlight, brh, DOTNET, #DOTNET, #SILVERLIGHT, #ASP.NET, Silverlight 4.0, ASP.NET, Two Way Binding, Data Binding,


Dinesh Sodani
30 · 6% · 1807
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

  • Hi Dinesh

    i have implement one Data grid in one page among this all Column generated dynamic among this, their is one column Amount. now if amount value is negative then will to change this particular datgridcell background property. i have to change background property at run time beacuse my datagrid column is autogenerated.

    i havee listen about DataTrigger, that will be used but i have no idea how to use it. if you know how to use DataTrigger or change background property logic, please provide guidance how to use this.

    commented on Nov 14 2011 11:52AM
    vasupatel
    2714 · 0% · 3

Your Comment


Sign Up or Login to post a comment.

"Data Binding in Silverlight" rated 5 out of 5 by 1 readers
Data Binding in Silverlight , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]