181

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>

?

1
  • 18
    if you need TwoWay Binding, you gotta use the second option Sep 23, 2011 at 7:01

6 Answers 6

216

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.

3
  • 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, 2010 at 7: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, 2014 at 0: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, 2020 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}"/>
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).

1
  • 1
    Thanks Miroslav, this was the problem I was encountering, switching to using TemplatedParent solved the problem.
    – MikeKulls
    Jul 4, 2011 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.

1

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

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.