Element to Element Binding in Silverlight

Posted By:Dhananjay KumarPosted Date:January 21, 2010Points:25
Category: SilverlightURL:http://www.dotnetspark.comIn this article I am going to discuss new feature of Silverlight 3.0 called Element to Element Binding. I will show three samples to understand Element to Element Binding.

Objective:

In this article I am going to discuss new feature of Silverlight 3.0 called Element to Element Binding. I will discuss three samples to understand Element to Element Binding.

Example

I will take an example to explain Element to Element binding. I will bind value of slider control to text of a text block. First I will bind using ValueChanged event of slider control (Sample #1) then I will move to accomplish same task using Element to Element Binding (Sample#2)

Element to Element Binding

This is a new feature provided by Silverlight 3.0. This allows one element to bind other element's property. This feature is very useful when you want to tie two objects such that when value of one objects changes it got reflected in other object's value.

Sample #1 [Event Handler Approach]

Take a Stack Panel

Create one Text Block and one Slider inside Stack Panel.

Add event handler for ValueChanged of slider

Assign value of slider to text of text box while handling the event ValueChanged

If we need to bind one slider control then above approach is fine but imagine if we need to bind many elements value to other elements value. In that scenario event handler approach is not quiet ok. So in Silverlight3.0 we got Element to Element Binding.

Sample #2 [Element to Element Binding]

This is new way of binding introduced in Silverlight 3.0. This is called Element to Element Binding. The best part of this approach is that rather than binding a property , we could bind an element to other element.

Element binding is done as; where bottomSlider is name of the element we are binding to text of text block. Text="{Binding Value,Mode=OneWay, ElementName=bottomSlider }

So we do not need any event handler in place for binding.

Output

Sample #3

Binding image width and height value with slider value.

In this sample I will add textbox with Event approach and height and width of image with Element to Element binding approach.