Click here to Skip to main content
Click here to Skip to main content

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

Share

About the Author

_ Kunal Chowdhury _
Technical Lead
India India
Kunal Chowdhury is a Microsoft "Client Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, 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 8 (WinRT). He posts his findings, articles in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionGood article. PinmemberminLVwang6-May-13 21:55 
GeneralMy vote of 4 PinmemberNagasundariS23-Aug-11 19:12 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 17 Aug 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid