TemplateBinding 標記延伸TemplateBinding Markup Extension

將控制項樣板中的屬性值連結成為樣板化控制項上另一個屬性的值。Links the value of a property in a control template to be the value of another property on the templated control.

XAML Attribute UsageXAML Attribute Usage

<object property="{TemplateBinding sourceProperty}" .../>  

XAML 屬性使用方式 (針對樣板或樣式中的 Setter 屬性)XAML Attribute Usage (for Setter property in template or style)

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

XAML 值XAML Values

propertyName 在 setter 語法中設定之屬性的 DependencyProperty.NameDependencyProperty.Name of the property being set in the setter syntax.
sourceProperty 存在於樣板化類型上的另一個相依性屬性,由其 DependencyProperty.Name 所指定。Another dependency property that exists on the type being templated, specified by its DependencyProperty.Name.

-或-- or -

「穿插句點」的屬性名稱,由樣板化目標類型以外的不同類型所定義。A "dotted-down" property name that is defined by a different type than the target type being templated. 這實際上是一個 PropertyPathThis is actually a PropertyPath. 請參閱PropertyPath XAML 語法See PropertyPath XAML Syntax.

備註Remarks

ATemplateBinding是的最佳化的形式繫結範本的情況下,類似於Binding建構{Binding RelativeSource={RelativeSource TemplatedParent}}A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource={RelativeSource TemplatedParent}}. TemplateBinding 永遠是單向繫結,即使相關的屬性是預設為雙向繫結也一樣。A TemplateBinding is always a one-way binding, even if properties involved default to two-way binding. 相關的兩個屬性必須是相依性屬性。Both properties involved must be dependency properties. 為了達到樣板化父代的雙向繫結下列繫結陳述式改為使用{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=MyDependencyProperty}In order to achieve two-way binding to a templated parent use the following binding statement instead {Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=MyDependencyProperty}.

RelativeSource是另一個標記延伸,有時候搭配使用或取而代之TemplateBinding才能執行相對屬性繫結,在範本內。RelativeSource is another markup extension that is sometimes used in conjunction with or instead of TemplateBinding in order to perform relative property binding within a template.

概念說明控制項樣板未涵蓋;如需詳細資訊,請參閱 < 控制項的樣式和範本Describing control templates as a concept is not covered here; for more information, see Control Styles and Templates.

屬性 (Attribute) 語法是最常搭配這個標記延伸來使用的語法。Attribute syntax is the most common syntax used with this markup extension. TemplateBinding 識別項字串後所提供的字串語彙基元,是指派做為基礎 Property 延伸類別的 TemplateBindingExtension 值。The string token provided after the TemplateBinding identifier string is assigned as the Property value of the underlying TemplateBindingExtension extension class.

物件項目語法雖然可行,但由於沒有實際的應用程式,所以這裡不加說明。Object element syntax is possible, but it is not shown because it has no realistic application. TemplateBinding 是用來以評估後的運算式填滿 setter 內的值,而使用 TemplateBinding 的物件項目語法來填滿 <Setter.Property> 屬性項目語法則太過繁瑣。TemplateBinding is used to fill values within setters, using evaluated expressions, and using object element syntax for TemplateBinding to fill <Setter.Property> property element syntax is unnecessarily verbose.

TemplateBinding 也可以用於會指定 Property 屬性 (Property) 做為 property=value 配對組的詳細屬性 (Attribute) 使用方式中。TemplateBinding can also be used in a verbose attribute usage that specifies the Property property as a property=value pair:

<object property="{TemplateBinding Property=sourceProperty}" .../>  

詳細使用方式通常是適用於具有一個以上可設定屬性或有些屬性為選擇性屬性的標記延伸。The verbose usage is often useful for extensions that have more than one settable property, or if some properties are optional. 因為 TemplateBinding 只有一個必要的可設定屬性,所以這種詳細使用方式並不常見。Because TemplateBinding has only one settable property, which is required, this verbose usage is not typical.

WPFWPF XAML 處理器實作中,這個標記延伸的處理由定義TemplateBindingExtension類別。In the WPFWPF XAML processor implementation, the handling for this markup extension is defined by the TemplateBindingExtension class.

TemplateBinding 是一種標記延伸。TemplateBinding is a markup extension. 如果必須將屬性 (Attribute) 值加上逸出符號,以免成為常值或處理常式名稱,而且這個動作必須更全面地實施 (而不是只對特定類型或屬性 (Property) 設定類型轉換子 (Type Converter)),則通常會實作標記延伸。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. 在 XAML 使用的所有標記延伸{}字元在其屬性語法中,這是用 XAML 處理器會辨識為標記延伸必須處理這個屬性的慣例。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. 如需詳細資訊,請參閱標記延伸和 WPF XAMLFor more information, see Markup Extensions and WPF XAML.

另請參閱See also