Binding from within control templates

A TemplateBinding is a particular type of binding that is used within ControlTemplate elements in order to data bind to the properties of the type that is being templated. It is very similar to the RelativeSource.TemplatedParent property that we discussed earlier:

<ControlTemplate x:Key="ProgressBar" TargetType="{x:Type ProgressBar}"> 
  ... 
  <TextBlock Text="{TemplateBinding Value}" /> 
  ... 
</ControlTemplate> 

In this example from earlier that we have edited slightly, we see that declaring a TemplateBinding is far more straightforward and less verbose than performing the same binding using the RelativeSource.TemplatedParent property. Let's remind ourselves what that looked like:

<TextBlock Text="{Binding Value, 
  RelativeSource={RelativeSource TemplatedParent}}" /> 

If possible, it is generally preferable to use a TemplateBinding instead of the RelativeSource.TemplatedParent property and although they perform the same connection in the binding, there are a few differences between them. For example, a TemplateBinding is evaluated at compile time, which enables faster instantiation of control templates, whereas a TemplatedParent binding is not evaluated until runtime.

Furthermore, it is a simpler form of binding and is missing a number of the Binding class properties, such as StringFormat and Delay. In addition, it places the extra constraints on the user, that it is permanently set to have a binding mode of OneWay and both binding target and binding source must be Dependency Properties. It was designed to be used in a single place with a single purpose and in that situation, it does its job well and more efficiently than its counterpart.