What is the template binding vs binding?

TemplateBinding is used for binding to the element properties within the template definition. In your example, you could have written:

 <Border Padding="{Binding Padding}" ...>

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.

88589 次浏览

Eren Ersönmenz already explained it quite well, but i would like to give it another perspective to better understand the concept.

  • It has much less functionality (no StringFormat, Delay, IsAsync, etc.. see the properties of Binding vs TemplateBindingExtention).
  • 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 a Rectangle for example. Now sometimes it is necessary for the ControlTemplate to actually use the properties of the logic part of a control. And thats what TemplateBinding is for it just tells the ControlTemplate "Use this property of the control we are giving the visual presentation". A good example is the Background property on every control, it has no meaning on its own, it gets its meaning by TemplateBinding it to child control in the ControlTemplate.

    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.

    Eren Ersönmenz already explained it quite well, but i would like to give it another perspective to better understand the concept.

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

    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 a Rectangle for example. Now sometimes it is necessary for the ControlTemplate to actually use the properties of the logic part of a control. And thats what TemplateBinding is for it just tells the ControlTemplate "Use this property of the control we are giving the visual presentation".

    EDIT: A good example is the Background property on every control, it has no meaning on its own, it gets its meaning by TemplateBinding it to child control in the ControlTemplate.

    Example:

    • A Button has a default ControlTemplate property and Height property
    • You override ControlTemplate property of a Button by writing your own (for example you want to make an Ellipse-looking button instead of Rectangle-looking)
    • 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.

    Example:

      I am using the query method of SQLiteDatabase. How do I use the query method?

    • A Button has a default ControlTemplate property and Height property
    • I tried this:

      Cursor cursor = sqLiteDatabase.query(
      tableName, tableColumns, whereClause, whereArgs, groupBy, having, orderBy);
      
    • You override ControlTemplate property of a Button by writing your own (for example you want to make an Ellipse-looking button instead of Rectangle-looking)
    • tableColumns - columns parameter is constructed as follows.

      String[] columns = new String[]{KEY_ID, KEY_CONTENT};
      
    • After you made an Ellipse in your new ControlTemplate, you want the Ellipse to be the same size as original Button's Height property
    • If we need to get all the fields, how should the column parameter to be constructed. Do we need to include all the Field Names in String array?

    • So you use TemplateBinding in order to reference Button's Height without naming itenter image description here