Click here to Skip to main content
Licence CPOL
First Posted 17 Aug 2011
Views 9,409
Bookmarked 1 time

RelativeSource Binding in Silverlight

By | 17 Aug 2011 | Technical Blog
Bind the same value twice in a control.
A Technical Blog article. View original blog here.[^]

Sometimes we need to bind the same data to different properties of the same control. For example, in some cases, we need to show text in a TextBlock and also want to show the same text as a tooltip to that TextBlock control.

How do we do it? It's very easy to bind the same value twice in the control. But is there any other way to implement this? Let us discuss this today in this blog post. If you were not aware of this till date, you will definitely like this.

Let us see the most common way to implement this. We can bind the same data to multiple properties of the same control like this:

<TextBlock Text="{Binding Information, ElementName=userControl}"
      ToolTipService.ToolTip="{Binding Information, ElementName=userControl}"
      HorizontalAlignment="Center" VerticalAlignment="Center"/>

If you want to do this in a similar way, you have to define a name for the UserControl or the TextBlock control itself and use that as the ElementName explicitly.

In other ways, you can use the RelativeSource binding in XAML to self bind the same data to multiple properties like Text and Tooltip, as below:

<TextBlock Text="{Binding Information, ElementName=userControl}"
    ToolTipService.ToolTip="{Binding Text, RelativeSource={RelativeSource Self}}"
    HorizontalAlignment="Center" VerticalAlignment="Center"/>

You will notice that the data binding mentioned here uses RelativeSource. Generally this property is used to bind one property of one object to another property of the same object like we defined in the above code snippet. Here, the word "Self" is a string token that has been set as the Mode of the RelativeSource binding.

Hope this will help you if you were not aware of it already. Thanks for reading this tip. Cheers. Download the source code of the entire solution from here: RelativeSource demo (source code) - 23 KB

Reference: Binding.RelativeSource Property (MSDN).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

_ Kunal Chowdhury _

Software Developer

India India

Member

Follow on Twitter Follow on Twitter
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberNagasundariS19:12 23 Aug '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Aug 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid