.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Element to Element Binding in Silverlight

Posted By:Dhananjay Kumar       Posted Date: January 21, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

In 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]
  1. Take a Stack Panel
  2. Create one Text Block and one Slider inside Stack Panel.

    
      
      
      
      
      
      
      
       
      
      
     
    
     
  3. Add event handler for ValueChanged  of slider
  4. Assign value of slider to text of text box while handling the event ValueChanged 

    namespace ElementBindingDemo
    {
      public partial class MainPage : UserControl
      {
      public MainPage()
      {
      InitializeComponent();
      topSlider.ValueChanged += new RoutedPropertyChangedEventHandler(topSlider_ValueChanged);
      }
      void topSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
      {
      topTextBlock.Text = topSlider.Value.ToString(); 
      }
      }
    }
Output
1.gif

Problem with above approach

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

2.gif

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. 

MainPage.Xaml


  
  
  
  
  
  
  
  
    
  


MainPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;  namespace ElementBindingDemo
{
  public partial class MainPage : UserControl
  {
  public MainPage()
  {
  InitializeComponent();
  topSlider.ValueChanged += new RoutedPropertyChangedEventHandler(topSlider_ValueChanged);
  }
  void topSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
  {
  //throw new NotImplementedException();'
  topTextBlock.Text = topSlider.Value.ToString(); 
  }
  }
}


Output

3.gif

Conclusion

In this article, I showed three samples on element to element binding. Thanks for reading.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend