180

What is the difference between these 2 bindings:

<ControlTemplate TargetType="{x:Type Button}">
   <Border BorderBrush="{TemplateBinding Property=Background}">
      <ContentPresenter />
   </Border>
</ControlTemplate>

and

<ControlTemplate TargetType="{x:Type Button}">
   <Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}">
      <ContentPresenter />
   </Border>
</ControlTemplate>

?

theSpyCry
  • 12,073
  • 28
  • 96
  • 152

6 Answers6

215

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
  • 2,630
  • 2
  • 17
  • 12
  • 18
    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 ? – theSpyCry Feb 18 '10 at 07:29
  • 3
    Also note that using Binding instead of TemplateBinding can have implications as to what you see during Design Time. In certain configurations, properties that are bound using {Binding RelativeSource...} won't appear in the designer (though they still show up during run time), but if you switch to using {TemplateBinding...} those properties are evaluated during Design Time. – lfalin Nov 12 '14 at 00:15
  • 1
    One thing I'll add in case it helps future visitors is that because TemplateBinding is evaluated at compile time you cannot use TemplateBinding to bind to a user defined attached property. In the case of user defined attached properties, you must use "{Binding RelativeSource={RelativeSource TemplatedParent} ... }" – MNB Mar 18 '20 at 18:59
38

TemplateBinding - More limiting than using regular Binding

  • More efficient than a Binding but it has less functionality
  • Only works inside a ControlTemplate's visual tree
  • Doesn't work with properties on Freezables
  • Doesn't work within a ControlTemplate's Trigger
  • Provides a shortcut in setting properties(not as verbose),e.g. {TemplateBinding targetProperty}

Regular Binding - Does not have above limitations of TemplateBinding

  • Respects Parent Properties
  • Resets Target Values to clear out any explicitly set values
  • Example: <Ellipse Fill="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background}"/>
Paul Fischer
  • 429
  • 4
  • 3
24

One more thing - TemplateBindings don't allow value converting. They don't allow you to pass a Converter and don't automatically convert int to string for example (which is normal for a Binding).

Miroslav Nedyalkov
  • 1,101
  • 1
  • 10
  • 22
  • 1
    Thanks Miroslav, this was the problem I was encountering, switching to using TemplatedParent solved the problem. – MikeKulls Jul 04 '11 at 23:46
17

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

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Nir
  • 29,306
  • 10
  • 67
  • 103
1

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

Yaz
  • 19
  • 1
0

They are used in a similar way but they have a few differences. Here is a link to the TemplateBinding documentation: http://msdn.microsoft.com/en-us/library/ms742882.aspx

David Rogers
  • 344
  • 2
  • 12