One of the more obscure features introduced in Silverlight 3 is the {RelativeSource} markup extension. It's poorer than its counterpart in WPF because the Silverlight version supports only two modes: Self and TemplatedParent. There are precious few examples out there demonstrating why you'd ever need {RelativeSource} in Silverlight. Here's one example.
Suppose you're building a custom control named SuperSlider that wraps (and presumably adds functionality to) the built-in Slider control, and that the default control template looks something like this:
<Style TargetType="local:SuperSlider">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:SuperSlider">
<Slider Value="{TemplateBinding Value}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
It looks reasonable, but try setting SuperSlider's Value property and you'll find that the thumb doesn't move. In addition, if the user slides the Slider control's thumb, SuperSlider's Value property doesn't change. Why? Because a TemplateBinding is inherently a 1-way binding. You need a 2-way binding to forge a robust connection between SuperSlider's Value property and the Slider's Value property.
In Silverlight 2, you had to write code to overcome this. In Silverlight 3, you can do it declaratively by combining {Binding} with {RelativeSource}:
<Style TargetType="local:SuperSlider">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:SuperSlider">
<Slider Value="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
That's one use for {RelativeSource} in Silverlight: creating 2-way template bindings. There are others, and for examples you need look no further than the default control templates for ListBox and a handful of other controls included in Silverlight 3.
On Nov 5 2009 5:00 PMBy jprosise
PingBack from http://isilverlight.cn/jeff-prosises-blog-silverlight-3s-new-relativesource-markup/
This post was mentioned on Twitter by seavista: Jeff Prosise's Blog : Silverlight 3's New {RelativeSource} Markup ... http://bit.ly/3ibndo
Can you please Give Code of this Example of this in C# for Code behind !?
Thank you
Raghuraman
Aaarg, this blue/red code coloring theme is killing my eyes :-]
Sorry about that. We recently deployed a new Web site with a whole new visual theme. However, our blog entries didn't get ported to the new theme, so we're having to go back and redo all the code samples one at a time. I've gone back a few months but haven't yet gotten to older entries like this one.
Did you not learn to use blue on a dark background in third grade?