views:

5558

answers:

4

What is the difference between

<ControlTemplate TargetType="{x:Type Button}" x:Key="buttonTemplate">
   <Border BorderBrush="{TemplateBinding Property=Background}" BorderThickness="3" >
      <ContentPresenter Margin="10"/>
   </Border>
</ControlTemplate>

and

<ControlTemplate TargetType="{x:Type Button}" x:Key="buttonTemplate">
   <Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" BorderThickness="3" >
      <ContentPresenter Margin="10"/>
   </Border>
</ControlTemplate>

?

+2  A: 

It is the same thing, TemplateBinding is just a shorter, optimized version, see: http://msdn.microsoft.com/en-us/library/ms742882.aspx

David Rogers
+4  A: 

TempleteBinding is a shorthand for Binding with TemplatedParent but it does not expose all the capabilities of the Binding class, for example you can't control Binding.Mode from TempleteBinding.

Nir
+18  A: 

TemplateBinding is not quite the same thing. MSDN docs are often written by people that have to quiz monosyllabic SDEs about software features, so the nuances are not quite right.

TemplateBindings are evaluated at compile time against the type specified in the control template. This allows for much faster instantiation of compiled templates. Just fumble the name in a templatebinding and you'll see that the compiler will flag it.

The binding markup is resolved at runtime. While slower to execute, the binding will resolve property names that are not visible on the type declared by the template. By slower, I'll point out that its kind of relative since the binding operation takes very little of the application's cpu. If you were blasting control templates around at high speed you might notice it.

As a matter of practice use the TemplateBinding when you can but don't fear the Binding.

Grant BlahaErath
So the main think to remember: Compile time vs Runtime. The TemplateBinding won't work if you try it to change during the runtime. Right ?
PaN1C_Showt1Me
A: 

I thought TemplateBinding does not support Freezable types (which includes brush objects). To get around the problem. One can make use of TemplatedParent

Yaz