TemplateBindingExtension Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Implements a markup extension that supports the binding between the value of a property in a template and the value of some other exposed property on the templated control.

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)


public class TemplateBindingExtension : MarkupExtension


Initializes a new instance of the TemplateBindingExtension class.


Initializes a new instance of the TemplateBindingExtension class with the specified dependency property that is the source of the binding.


Gets or sets the converter that interprets between source and target of a binding.


Gets or sets the parameter to pass to the converter.


Gets or sets the property being bound to.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns an object that should be set as the value on the target object's property for this markup extension. For TemplateBindingExtension, this is an expression (TemplateBindingExpression) that supports the binding. (Overrides MarkupExtension.ProvideValue(IServiceProvider).)


Returns a string that represents the current object.(Inherited from Object.)

You use TemplateBinding in template to bind to a value on the control the template is applied to. A TemplateBinding is more efficient than a Binding but it has less functionality. Using a TemplateBinding is equivalent to using a Binding with the RelativeSource property set to RelativeSource.TemplatedParent.

The following example shows a ControlTemplate that defines a ListBox that is horizontal and has rounded corners. The TemplateBinding indicates that the Background of the Border should be synchronized with the Background value that is set on the ListBox. You use TemplateBinding in your ControlTemplate when you want to give the user of your control the control over the values of certain properties. For a discussion of this example, see Styling and Templating.

The following example shows the ControlTemplate of the Label control. The HorizontalAlignment and VerticalAlignment properties are bound to the values of the HorizontalContentAlignment and VerticalContentAlignment properties of the Label control that this ControlTemplate is applied to.

<Style x:Key="{x:Type Label}"
  <Setter Property="HorizontalContentAlignment"
          Value="Left" />
  <Setter Property="VerticalContentAlignment"
          Value="Top" />
  <Setter Property="Template">
      <ControlTemplate TargetType="Label">
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            RecognizesAccessKey="True" />
          <Trigger Property="IsEnabled"
            <Setter Property="Foreground">
                <SolidColorBrush Color="{DynamicResource DisabledForegroundColor}" />

.NET Framework
Available since 3.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top