TemplateBinding is used to bind to the properties of an element in a template definition. In your example, you could write
<Border Padding="{Binding Padding}" ...>
means to bind the border fill property to the padding property ... what? You want to say: "The padding property of the control for which this template is used." You cannot name this name because at this time you do not know x: The name of the control (even if you did, this would not work because it was in a different type). However, you can do this by specifying a relative source
<Border Padding="{Binding Padding, RelativeSource={RelativeSource TemplatedParent}" ...>
or use TemplateBinding which is a shortcut (*) for above
<Border Padding="{TemplateBinding Padding}" ...>
(*) In addition to being too verbose in these template scripts, TemplateBinding has a couple of differences over regular binding:
- It is calculated at compile time. (If, for example, the Padding property did not exist, you would get a compilation error. But if you should use the binding with TemplatedParent, you will only see an error at runtime.)
- This is always one-way binding.
- This requires that both the source and target properties be dependent properties.
- It has much less functionality (without StringFormat, Delay, IsAsync, etc. see the properties of Binding vs TemplateBindingExtention ).
Eren Ersönmez May 15 '12 at 9:25 a.m. 2012-05-15 09:25
source share