Export (0) Print
Expand All

TemplateBindingExtension Class

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.

System.Object
  System.Windows.Markup.MarkupExtension
    System.Windows.TemplateBindingExtension

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[TypeConverterAttribute(typeof(TemplateBindingExtensionConverter))]
[MarkupExtensionReturnTypeAttribute(typeof(Object))]
public class TemplateBindingExtension : MarkupExtension
For XAML information, see TemplateBinding Markup Extension.

The TemplateBindingExtension type exposes the following members.

  NameDescription
Public methodTemplateBindingExtension()Initializes a new instance of the TemplateBindingExtension class.
Public methodTemplateBindingExtension(DependencyProperty)Initializes a new instance of the TemplateBindingExtension class with the specified dependency property that is the source of the binding.
Top

  NameDescription
Public propertyConverterGets or sets the converter that interprets between source and target of a binding.
Public propertyConverterParameterGets or sets the parameter to pass to the converter.
Public propertyPropertyGets or sets the property being bound to.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodProvideValueReturns 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).)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

<Style TargetType="ListBox">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ListBox">
        <Border CornerRadius="5" Background="{TemplateBinding ListBox.Background}">
          <ScrollViewer HorizontalScrollBarVisibility="Auto">
            <StackPanel Orientation="Horizontal"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       IsItemsHost="True"/>
          </ScrollViewer>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

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}"
       TargetType="Label">
  <Setter Property="HorizontalContentAlignment"
          Value="Left" />
  <Setter Property="VerticalContentAlignment"
          Value="Top" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Label">
        <Border>
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            RecognizesAccessKey="True" />
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsEnabled"
                   Value="false">
            <Setter Property="Foreground">
              <Setter.Value>
                <SolidColorBrush Color="{DynamicResource DisabledForegroundColor}" />
              </Setter.Value>
            </Setter>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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