Click here to Skip to main content
13,446,125 members (36,788 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 17 Aug 2011

RelativeSource Binding in Silverlight

, 17 Aug 2011
Rate this:
Please Sign up or sign in to vote.
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:

<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.


Download the source code of the entire solution from RelativeSource demo (source code) - 23 KB



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


About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer 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 Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:

You may also be interested in...

Comments and Discussions

QuestionGood article. Pin
minLVwang6-May-13 21:55
memberminLVwang6-May-13 21:55 
GeneralMy vote of 4 Pin
NagasundariS23-Aug-11 19:12
memberNagasundariS23-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180314.2 | Last Updated 17 Aug 2011
Article Copyright 2011 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid