This is a mirror of official site: http://jasper-net.blogspot.com/

RelativeSource Binding in Silverlight

| Thursday, August 18, 2011
Sometime we need to bind the same data in 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 to do it? It's very easy to bind the same value twice in the control. But is there any other way to implement the same? Let us discuss this today in this blog post. If you are not aware of it till date, you will definitely like.

Let us discuss the common way to implement this. As we discussed we can bind data in 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 similar way, you have to define a name to the UserControl or to the TextBlock control itself and use that as "ElementName" explicitly.

In other way, you can use the RelativeSource binding in XAML to self binding the same data in 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"/>


Read more: Kunal's Blog
QR: relativesource-binding-in-silverlight.html

Posted via email from Jasper-net

0 comments: