Silverlight 3: Template Binding vs. Relative Binding

Playing with SL3, I did a small research to find differences between Relative Binding and TemplateBinding in SL3 when used in generic templates:

 

· Automatic Conversion is not supported in template binding, so with priority being int type, that won’t work

 

<TextBlock Text="{TemplateBinding Priority}"/>

 

But this workaround will:

 

<TextBlock DataContext="{TemplateBinding Priority} Text="{Binding}"/>

 

· Similar solution to the above must be used (still in SL3) to apply a converter:

 

<TextBlock DataText="{TemplateBinding Priority} Text="{Binding, Converter={StaticResource PriorityConverter}}"/>

 

, while it is more straight forward (and longer) with relative binding

 

Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Priority, Converter={StaticResource PriorityConverter}}"/>

 

· While template binding is a rough equivalent to Binding with RelativeSource={RelativeSource TemplatedParent}, there are additional differences:

o Template Binding is simpler and (usually unnoticeably) faster

o Template Binding is always one-way - see also Bea Stollnitz blog

o TemplateBinding does not support context inheritance (What is context inheritance is well described in Nick’s blog entry here), so something like this (Angle) won’t work:

 

                        <Viewbox Stretch="Uniform" RenderTransformOrigin="0.5,0.5">

                            <Viewbox.RenderTransform>

                                <TransformGroup>

                                    <RotateTransform Angle="{TemplateBinding FontSize}"/>

                                </TransformGroup>

                            </Viewbox.RenderTransform>

                               

Ok – the last point is only for WPF. There is no context inheritance in Silverlight 3. To be decided if that will be the case in SL 4 – keep you posted. It is a pity as it would bring an enormous value to generic control development allowing more things to be moved to XAML side.