One way, Two way and One time bindings using SilverLight

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 PDF’s from my site http://www.questpond.com

Other Silverlight FAQ

In case you are a complete fresher to silverlight then below are some silverlight FAQ’s  which can give you a quick start in this topic.

Silverlight FAQ Part 1:- http://www.codeproject.com/KB/WPF/WPFSilverLight.aspx  This tutorial has 21 basic FAQ’s which will help you understand WPF, XAML, help your build your first silverlight application and also explains the overall silverlight architecture.

SilverLight FAQ Part 2 (Animations and Transformations):- SilverLightFAQPart2.aspx This tutorial has 10 FAQ questions which starts with silverlight animation fundamentals and then shows a simple animated rectangle. The article then moves ahead and talks about 4 different ways of transforming the objects.

One way bindings

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.

clip_image001

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 as ‘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 silver light UI text box. To bind the property you need to specify ‘Binding Path=Year’ in the text property of the text box UI object. ‘Year’ is the property which we are binding with the text box UI object.

 

The final step is to bind the text box 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 ensure 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.

clip_image002

To implement two way binding there are two extra steps with addition to the steps provided for ‘OneWay’. The first change is we need to specify the mode as ‘TwoWay’ as shown in the below XAML code snippet.

 

Second change is we need to implement ‘INotifyPropertyChanged’ interface. Below is the class which shows how to implement the ‘INotifyPropertyChanged’ interface. Please note 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.

 

VerticalAlignment="Center" HorizontalAlignment="Center">

Simple demonstration of OneWay and TwoWay

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

clip_image003

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.

Enter your age in the below text box

 

Your approximate birth date

 

Source Code

You can get the Source Code here.

Original link:

http://www.codeproject.com/KB/silverlight/SilverlightBindings.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值