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 属性用法XAML 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 在资源库语法中设置的属性的 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 -

由要模板化的目标类型之外的类型所定义的“dotted-down”属性名称。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

一个TemplateBinding是的优化的形式绑定对于模板方案,类似于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.

描述控件模板作为一个概念是此处未介绍;有关详细信息,请参阅Control 样式和模板Describing control templates as a concept is not covered here; for more information, see Control Styles and Templates.

特性语法是最常用于该标记扩展的语法。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 用于使用计算的表达式来填充资源库内的值,因此使用 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=value 对: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.

在中WPFWPFXAML 处理器实现中,对此标记扩展的处理由定义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. 当要求转义特性值应为非文本值或非处理程序名称时,通常会实现标记扩展,相对于只在某些类型或属性上放置类型转换器而言,此需求更具有全局性。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