Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

RelativeSource Binding in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.89/5 (2 votes)
17 Aug 2011CPOL1 min read 25.7K   1   2
Bind the same value twice in a control

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:

XML
<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:

XML
<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 RelativeSource demo (source code) - 23 KB

Reference

This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

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


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
QuestionGood article. Pin
minLVwang6-May-13 21:55
minLVwang6-May-13 21:55 
GeneralMy vote of 4 Pin
NagasundariS23-Aug-11 19:12
NagasundariS23-Aug-11 19:12 

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

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