I could not understand BorderThickness="{TemplateBinding BorderThickness}
.
Here the code:
<ControlTemplate TargetType="{x:Type wpftoolkit:DataGridCell}">
<Border Padding="{TemplateBinding Padding}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
Also please explain other types of binding.
A picture is worth a 1000 words. In this case it is 7 minutes video: https://www.youtube.com/watch?v=z-0TZR-7xLI
EDIT: Example:
Button
has a defaultControlTemplate
property andHeight
propertyControlTemplate
property of aButton
by writing your own (for example you want to make anEllipse
-looking button instead ofRectangle
-looking)Ellipse
in your newControlTemplate
, you want theEllipse
to be the same size as original Button'sHeight
propertyTemplateBinding
in order to referenceButton
'sHeight
without naming itEren Ersönmenz already explained it quite well, but i would like to give it another perspective to better understand the concept.
In WPF every control is more or less detached from its presentation. You can always change the template of controls and make it look completely different. A button works as expected with a
ControlTemplate
only consisting of aRectangle
for example. Now sometimes it is necessary for theControlTemplate
to actually use the properties of the logic part of a control. And thats whatTemplateBinding
is for it just tells theControlTemplate
"Use this property of the control we are giving the visual presentation". A good example is theBackground
property on every control, it has no meaning on its own, it gets its meaning byTemplateBinding
it to child control in theControlTemplate
.Binding on its own is very good described in the MSDN. This is a very nice cheat sheet which in fact hangs on my wall right next to me. It gives a good overview of all the different bindings available.
From TemplateBinding Markup Extension,
TemplateBinding
links the value of a property in a control template to the value of some other exposed property on the templated control. Other words, it is for binding values in a template.Binding connects a property of binding targets and data sources.
TemplateBinding is used for binding to the element properties within the template definition. In your example, you could have written
meaning to bind the border's padding property to the padding property of... what? You'd like to say, "padding property of the control that this template is being used for." You can't give it a name because you don't know the x:Name of the control at this time (even if you did, it wouldn't work because its in a different namescope). However, you can do this by defining a relative source
or use TemplateBinding which is a shortcut(*) for above
(*) In addition to being less verbose in these templating scenarios, TemplateBinding has a couple of differences compared to a regular binding: