Binding in a Control Template
When you're creating a control template and you want to bind to the templated parent you can use:
1
<TextBlock Name="tb" Text="{TemplateBinding Caption}"/>
2
​
3
<!-- Which is the same as -->
4
<TextBlock Name="tb" Text="{Binding Caption, RelativeSource={RelativeSource TemplatedParent}}"/>
Copied!
Although the two syntaxes shown here are equivalent in this case, there is a difference. TemplateBinding accepts only a single property rather than a property path, so if you want to bind using a property path you must use the second syntax:
1
<!-- This WON'T work as TemplateBinding only accepts single properties -->
2
<TextBlock Name="tb" Text="{TemplateBinding Caption.Length}"/>
3
​
4
<!-- Instead this syntax must be used in this case -->
5
<TextBlock Name="tb" Text="{Binding Caption.Length, RelativeSource={RelativeSource TemplatedParent}}"/>
Copied!
Last modified 6mo ago
Export as PDF
Copy link