共用方式為


TemplateBinding Markup Extension

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Links the value of a property in a control template to the value of some other exposed property on the templated control. TemplateBinding can only be used within a ControlTemplate definition in XAML.

XAML Attribute Usage

<object propertyName="{TemplateBinding targetProperty}" .../>

XAML Attribute Usage (for Setter property in template or style)

<Setter Property="propertyName" Value="{TemplateBinding targetProperty}" .../>

XAML Values

propertyName

The name of the property being set in the setter syntax. This must be a dependency property.

targetProperty

The name of a dependency property that exists on the type being templated.

Remarks

In the Silverlight XAML processor implementation, there is no backing class representation for TemplateBinding. TemplateBinding is exclusively for use in XAML markup, using the {} syntax that indicates to a XAML processor that the contents should be treated by a markup extension.

Attempting to use a TemplateBinding outside of a ControlTemplate definition in XAML will result in a parser error.

It is a fairly common usage for propertyName and targetProperty to use the same property name. In this case, a control might define a property on itself and forward the property to an existing and intuitively named property of one of its component parts. For example, a control that incorporates a TextBlock in its compositing, which is used to display the control's own Text property, might include this XAML as a part in the control template:

<TextBlock Text="{TemplateBinding Text}" .... />
NoteNote:

If you are familiar with WPF data binding, WPF supports property path techniques for retrieving subproperties, getting indexed items in a collection, and retrieving attached property values. Silverlight currently does not provide this support. A Silverlight template target property must be a property that exists on the templated parent class.

TemplateBinding is a markup extension. Markup extensions are typically implemented when there is a requirement to escape attribute values to be other than literal values or handler names, and the requirement is more global than just putting type converters on certain types or properties. All markup extensions in XAML use the { and } characters in their attribute syntax, which is the convention by which a XAML processor recognizes that a markup extension must process the attribute.