What TemplateBinding Does
{TemplateBinding} is a lightweight, one-way binding used exclusively inside a ControlTemplate to connect a visual element in the template to a property of the control being templated, such as wiring a template's inner Border's Background to the templated Button's own Background property. It exists as a specialized, cheaper alternative to writing {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}, which achieves the same conceptual result but with more overhead because it goes through the full data-binding engine rather than the streamlined template binding pathway.
Cricket analogy: It is like a substitute fielder mirroring the exact hand signals given by the captain without a full radio relay through the coaching staff, a faster and lighter communication path than the general chain of command, just as TemplateBinding is a lighter pathway than a full RelativeSource Binding.
TemplateBinding Limitations vs. RelativeSource TemplatedParent
TemplateBinding only supports one-way, source-to-target data flow and cannot be used with a Converter that needs multi-value input, and it cannot be applied to a property inside a Style Setter's Value in older XAML processors, whereas {Binding RelativeSource={RelativeSource TemplatedParent}} supports the full binding feature set, including TwoWay mode, value converters, and multi-binding. In modern WPF, TemplateBinding is essentially syntactic sugar that the compiler converts into an optimized templated-parent binding, so the practical difference is mostly about brevity and minor performance in the common one-way template scenarios.
Cricket analogy: It is like a specialist pinch-runner brought on purely to sprint bases, unable to bat or field the way the full-time player they replaced could, just as TemplateBinding trades full binding features for a narrower, faster one-way role.
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="4">
<ContentPresenter HorizontalAlignment="{TemplateBinding
HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>TemplateBinding can only be used directly inside a ControlTemplate applied to elements in that template's visual tree; using it inside a DataTemplate, or trying to feed it through a Style Setter's Value where the templated parent context is not available, will fail or silently produce no value in some XAML processors — use {Binding RelativeSource={RelativeSource TemplatedParent}} in those cases instead.
- TemplateBinding connects a property inside a ControlTemplate to a property of the control being templated.
- It is a lighter-weight, one-way-only alternative to {Binding RelativeSource={RelativeSource TemplatedParent}}.
- TemplateBinding does not support TwoWay mode or multi-value converters.
- In modern WPF it largely compiles down to an optimized TemplatedParent binding under the hood.
- It only works inside a ControlTemplate's visual tree, not inside a DataTemplate.
- Common uses include binding Background, BorderBrush, BorderThickness, and Padding from the control onto template visuals.
- When TemplateBinding's limitations are hit, falling back to RelativeSource TemplatedParent restores full binding capability.
Practice what you learned
1. Where can {TemplateBinding} be used?
2. Which binding direction does TemplateBinding support?
3. What is the more verbose but fully-featured equivalent of {TemplateBinding Background}?
4. What happens if you try to use TemplateBinding inside a DataTemplate instead of a ControlTemplate?
5. Why might a developer prefer TemplateBinding over RelativeSource TemplatedParent for a simple Background hookup?
Was this page helpful?
You May Also Like
The Binding Markup Extension
How {Binding} connects UI properties to data-source properties, including Path, Mode, and source-resolution options.
Markup Extensions Overview
How XAML's curly-brace syntax defers value creation to parse time through classes that implement MarkupExtension.
StaticResource and DynamicResource
The difference between resolving a resource once at parse time versus keeping a live, updating reference to it.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics