0% found this document useful (0 votes)
59 views

One Way, Two Way and One Time Bindings Using Silverlight: DVD Title Available

This document summarizes the different types of bindings in Silverlight - one way, two way, and one time bindings. It explains that one way binding only allows data flow from the object to the UI, two way binding allows synchronization between the UI and object in both directions, and one time binding only populates the UI once without further tracking. It provides a code example to demonstrate one way and two way bindings using a simple Silverlight application with two text boxes, one bound to an age property with two way binding and the other displaying the calculated birth year.

Uploaded by

pkchawla19792473
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

One Way, Two Way and One Time Bindings Using Silverlight: DVD Title Available

This document summarizes the different types of bindings in Silverlight - one way, two way, and one time bindings. It explains that one way binding only allows data flow from the object to the UI, two way binding allows synchronization between the UI and object in both directions, and one time binding only populates the UI once without further tracking. It provides a code example to demonstrate one way and two way bindings using a simple Silverlight application with two text boxes, one bound to an age property with two way binding and the other displaying the calculated birth year.

Uploaded by

pkchawla19792473
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Get QuestPond’s DVD at the following book shops:

Computer Book Shop, Mumbai, Fort. Ph No: +91‐22‐22070989/6356, 66317922,


66317923/24. E‐mail: ‐ 
[email protected]

Dharma Enterprises, Mumbai, Fort. Ph No: +91‐22‐22611760, 65718637, 64512196.


E‐mail: ‐
[email protected] [email protected]

GreenLeaf Book Shop, Mumbai. Ph No: +91‐22‐28770081, 28786269 Email: ‐


[email protected]

Varsha Book Depot, Mumbai, Vashi. Ph No: +91‐22‐27896049, 27897079, 27894475.


Email: ‐
[email protected], [email protected]

Distributor in Hyderabad, Ph No: +91‐40‐23080615, 09391342107, 09866068037. Email:



[email protected]

Rajkamal Book Shop, Hyderabad Ph No: +91‐040‐24756064/


27802113/32453620/24754671/24756064.
Rajlaxmi Book House, Hyderabad Ph No: +91‐040‐66789218.

DVD Title available: ‐


.NET Video Pack @ 1700 INR all videos from QuestPond including one month subscription
program fordetails of the contents log‐on to www.questpond.com

.NET Projects DVD @ 600 INR includes Invoicing Application end to end project using SDLC
cycle for details of the contents log‐on to www.questpond.com

Sharepoint, WCF, WPF, WWF, LINQ & SharePoint Videos @ 1000 INR for details of the
contents log‐on to www.questpond.com 

One Way, Two Way and One Time Bindings using Silverlight

• Introduction
• One Way Binding
• Two Way Binding
• One Time Binding
• Simple Demonstration of OneWay and TwoWay
Introduction

This article will talk about three ways of binding object properties with Silverlight user
interfaces. We will first go through the fundamentals of the 3 bindings and then take up a
small sample which will demonstrate how the binding works. We have also attached the
source for the same.

I have collected around 400 FAQ questions and answers in WCF, WPF, WWF, SharePoint,
design patterns, UML, etc. Feel free to download these FAQ PDFs from
http://www.questpond.com.

One Way Binding

As the name, so the behavior. In one way bindings, data flows only from object to UI and
not vice-versa. For instance, you can have a textbox called as TxtYear which is binded
with an object having property Year. So when the object value changes, it will be reflected
on the Silverlight UI, but the UI cannot update the year property in the object.

It is a three step procedure to implement one way binding. First, create your class which
you want to bind with the Silverlight UI. For instance, below is a simple class called
ClsDate with a Year property.

public class clsDate


{
private int _intYear;
public int Year
{
set
{
_intYear = value;
}
get
{
return _intYear;
}
}
}

In the second step, you need to tie up the Year property with a Silverlight UI textbox. To
bind the property, you need to specify ‘Binding Path=Year’ in the text property of the
textbox UI object. Year is the property which we are binding with the textbox UI
object.

<TextBox x:Name="txtCurrentYear" Text="{Binding Path=Year}"


Height="30" Width="150" VerticalAlignment="Center"
HorizontalAlignment="Center"></TextBox>

The final step is to bind the textbox data context with the date object just created.

public partial class Page : UserControl


{
public Page()
{
InitializeComponent();
clsDate objDate = new clsDate();
objDate.Year = DateTime.Now.Year;
txtCurrentYear.DataContext = objDate;
}
}

Two Way Binding

Two way binding ensures data synchronization of data between UI and objects. So any
change in object is reflected to the UI and any change in UI is reflected in the object.
To implement two way binding, there are two extra steps in addition to the steps provided
for OneWay. The first change is that we need to specify the mode as TwoWay as shown in
the below XAML code snippet.

<TextBox x:Name="txtEnterAge" Text="{Binding Path=Age, Mode=TwoWay}"


Height="30" Width="150" VerticalAlignment="Center"
HorizontalAlignment="Center"></TextBox>

The second change is that we need to implement INotifyPropertyChanged interface.


Below is the class which shows how to implement the INotifyPropertyChanged
interface. Please note that you need to import System.ComponentModel namespace.

public class clsDate : INotifyPropertyChanged


{
public event PropertyChangedEventHandler PropertyChanged;
private int _intYear;
public int Year
{
set
{
_intYear = value;
OnPropertyChanged("Year");
}
get
{
return _intYear;
}
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(property));
}
}
}

The binding of data with data context is a compulsory step which needs to be performed.

One Time Binding

In one time binding, data flows from object to the UI only once. There is no tracking
mechanism to update data on either side. One time binding has marked performance
improvement as compared to the previous two bindings discussed. This binding is a good
choice for reports where the data is loaded only once and viewed.
<TextBox x:Name="txtEnterAge" Text="{Binding Path=Age, Mode=OneTime}"
Height="30" Width="150"
VerticalAlignment="Center" HorizontalAlignment="Center"></TextBox>

Simple Demonstration of OneWay and TwoWay

Below is a simple sample code wherein we have two text boxes; one takes in the age and
the other text box calculates the approximate birth date.

Below is a simple class which has both the properties. We have implemented
INotifyPropertyChanged interface so that we can have two way communication for the
year property.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
//
using System.ComponentModel;
namespace SilverLightBinding
{
public class clsDate : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private int _intYear;


private int _intAge;
public int Year
{
set
{
_intYear = value;
OnPropertyChanged("Year");

}
get
{
return _intYear;
}

}
public int Age
{
set
{
_intAge = value;
Year = DateTime.Now.Year - _intAge;
}
get
{
return _intAge;
}
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(property));
}
}

}
}

Finally we have also binded the Silverlight UI objects with the class properties. Below is the
XAML snippet for the same. One point to be noted is that Age is bounded using two way
mode as we need to modify the same from the user interface.

<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">


Enter your age in the below text box</TextBlock>
<TextBox x:Name="txtEnterAge" Text="{Binding Path=Age, Mode=TwoWay}"
Height="30" Width="150" VerticalAlignment="Center"
HorizontalAlignment="Center"></TextBox>

<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">


Your approximate birth date</TextBlock>

<TextBox x:Name="txtCurrentYear" Text="{Binding Path=Year}"


Height="30" Width="150" VerticalAlignment="Center"
HorizontalAlignment="Center"></TextBox>

You might also like