What is template binding to binding?

I could not understand BorderThickness="{TemplateBinding BorderThickness} . Here is 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> 

Please also explain other types of bindings.

+79
wpf mvvm-light
May 15 '12 at 9:08
source share
4 answers

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 ).
+144
May 15 '12 at 9:25
source share

Eren Ersönmenz already explained this quite well, but I would like to give him another perspective in order to better understand the concept.

In WPF, each control is more or less disconnected from its presentation. You can always change the control template and make it completely different. The button works as expected with a ControlTemplate consisting only of a Rectangle . Now sometimes for a ControlTemplate it is necessary to use the properties of the logical part of the control. And thats that the TemplateBinding for it simply tells the ControlTemplate "Use this property of the control that we give the visual representation." A good example is the Background property on each control, it does not make sense on its own, it has a TemplateBinding value for the child control in the ControlTemplate .

Binding itself is very well described on MSDN . This is a very good deceiver who actually hangs on my wall right next to me. It gives a good overview of all available bindings available.

+26
May 15 '12 at 9:33
source share

A picture is worth 1000 words. In this case, this is 7 minutes of video: https://www.youtube.com/watch?v=z-0TZR-7xLI

EDIT: Example:

  • A Button has a default ControlTemplate property and a Height property
  • You override the ControlTemplate property of the Button object by writing your own (for example, you want to make the Ellipse -looking button instead of the Rectangle -looking button)
  • After you created Ellipse in your new ControlTemplate , you want the Ellipse be the same size as the original Height button property
  • So you use TemplateBinding to refer to Button Height without naming it enter image here
+23
Mar 11 '16 at 16:23
source share

From the Extension of TemplateBinding extensions , TemplateBinding binds the value of a property in a control template with the value of some other public property for template control. In other words, this is the binding value in the template.

Binding to connect the property of binding targets and data sources.

+4
May 15 '12 at 9:29 a.m.
source share



All Articles