绑定标记扩展Binding Markup Extension

将属性值延迟为数据绑定值,创建中间表达式对象并在运行时解释应用于该元素及其绑定的数据上下文。Defers a property value to be a data-bound value, creating an intermediate expression object and interpreting the data context that applies to the element and its binding at run time.

绑定表达式使用情况Binding Expression Usage

<object property="{Binding}" .../>  
-or-  
<object property="{Binding  bindProp1=value1[, bindPropN=valueN]*}" ...  
/>  
-or-  
<object property="{Binding path}" .../>  
-or  
<object property="{Binding path[, bindPropN=valueN]*}" .../>  

语法说明Syntax Notes

在这些语法中,[]* 不是文本。In these syntaxes, the [] and * are not literals. 它们是表示法的一部分,用于指示可以使用零个或多个bindProp=对,并在它们和前面的bindProp=对之间使用 , 分隔符。They are part of a notation to indicate that zero or more bindProp=value pairs can be used, with a , separator between them and preceding bindProp=value pairs.

"可以使用绑定扩展设置的绑定属性" 一节中列出的任何属性都可以通过使用 Binding 对象元素的属性来设置。Any of the properties listed in the "Binding Properties That Can Be Set with the Binding Extension" section could instead be set using attributes of a Binding object element. 但是,这并不是真正 Binding的标记扩展用法,只是设置 CLR Binding 类的属性的属性的常规 XAML 处理。However, that is not truly the markup extension usage of Binding, it is just the general XAML processing of attributes that set properties of the CLR Binding class. 换句话说,<Binding bindProp1="value1"[ bindPropN="valueN"]*/>Binding 对象元素用法的属性的等效语法,而不是 Binding 表达式用法。In other words, <Binding bindProp1="value1"[ bindPropN="valueN"]*/> is an equivalent syntax for attributes of Binding object element usage instead of a Binding expression usage. 若要了解 Binding的特定属性的 XAML 属性用法,请参阅 .NET Framework 类库中 Binding 相关属性的 "XAML 属性用法" 部分。To learn about the XAML attribute usage of specific properties of Binding, see the "XAML Attribute Usage" section of the relevant property of Binding in the .NET Framework Class Library.

XAML 值XAML Values

bindProp1, bindPropN 要设置的 BindingBindingBase 属性的名称。The name of the Binding or BindingBase property to set. 并非所有 Binding 属性都可以使用 Binding 扩展进行设置,而某些属性只能通过使用更多嵌套标记扩展在 Binding 表达式中设置。Not all Binding properties can be set with the Binding extension, and some properties are settable within a Binding expression only by using further nested markup extensions. 请参阅 "绑定可通过绑定扩展进行设置的属性" 一节。See "Binding Properties That Can Be Set with the Binding Extension" section.
value1, valueN 要将属性设置为的值。The value to set the property to. 特性值的处理最终特定于所设置的特定 Binding 属性的类型和逻辑。The handling of the attribute value is ultimately specific to the type and logic of the specific Binding property being set.
path 设置隐式 Binding.Path 属性的路径字符串。The path string that sets the implicit Binding.Path property. 另请参阅PROPERTYPATH XAML 语法See also PropertyPath XAML Syntax.

非限定 {Binding}Unqualified {Binding}

"绑定表达式使用情况" 中显示的 {Binding} 用法使用默认值创建 Binding 对象,其中包括 null的初始 Binding.PathThe {Binding} usage shown in "Binding Expression Usage" creates a Binding object with default values, which includes an initial Binding.Path of null. 这在许多情况下仍有用,因为创建的 Binding 可能依赖于关键数据绑定属性,如在运行时数据上下文中设置 Binding.PathBinding.SourceThis is still useful in many scenarios, because the created Binding might be relying on key data binding properties such as Binding.Path and Binding.Source being set in the run-time data context. 有关数据上下文概念的详细信息,请参阅数据绑定For more information on the concept of data context, see Data Binding.

隐式路径Implicit Path

Binding 标记扩展使用 Binding.Path 作为概念 "默认属性",其中 Path= 不需要出现在表达式中。The Binding markup extension uses Binding.Path as a conceptual "default property", where Path= does not need to appear in the expression. 如果使用隐式路径指定 Binding 表达式,则隐式路径必须出现在表达式中的第一个位置,在任何其他 bindProp=value 对(其中,Binding 属性由 name 指定)之前。If you specify a Binding expression with an implicit path, the implicit path must appear first in the expression, prior to any other bindProp=value pairs where the Binding property is specified by name. 例如: {Binding PathString},其中,PathString 是一个字符串,其计算结果为标记扩展用法创建的 BindingBinding.Path 的值。For example: {Binding PathString}, where PathString is a string that is evaluated to be the value of Binding.Path in the Binding created by the markup extension usage. 可以在逗号分隔符后面追加具有其他命名属性的隐式路径,例如 {Binding LastName, Mode=TwoWay}You can append an implicit path with other named properties after the comma separator, for example, {Binding LastName, Mode=TwoWay}.

可通过绑定扩展进行设置的绑定属性Binding Properties That Can Be Set with the Binding Extension

本主题中所示的语法使用泛型 bindProp=value 近似值,因为可以通过 Binding 标记扩展/表达式语法来设置 BindingBaseBinding 的多个读/写属性。The syntax shown in this topic uses the generic bindProp=value approximation, because there are many read/write properties of BindingBase or Binding that can be set through the Binding markup extension / expression syntax. 它们可以按任意顺序进行设置,但隐含 Binding.Path例外。They can be set in any order, with the exception of an implicit Binding.Path. (您可以选择显式指定 Path=,在这种情况下,可以按任意顺序进行设置)。(You do have the option to explicitly specify Path=, in which case it can be set in any order). 基本上,可以使用以逗号分隔的 bindProp=value 对,在下面的列表中设置零个或多个属性。Basically, you can set zero or more of the properties in the list below, using bindProp=value pairs separated by commas.

其中一些属性值需要对象类型,这些对象类型不支持 XAML 中的文本语法的本机类型转换,因此需要标记扩展才能设置为属性值。Several of these property values require object types that do not support a native type conversion from a text syntax in XAML, and thus require markup extensions in order to be set as an attribute value. 有关详细信息,请查看每个属性的 .NET Framework 类库中的 XAML 特性用法部分;用于带有或不带进一步标记扩展用法的 XAML 属性语法的字符串基本上与您在 Binding 表达式中指定的值相同,但不会将每个 bindProp的引号括起来 =value``Binding 表达式。Check the XAML Attribute Usage section in the .NET Framework Class Library for each property for more information; the string you use for XAML attribute syntax with or without further markup extension usage is basically the same as the value you specify in a Binding expression, with the exception that you do not place quotation marks around each bindProp=value in the Binding expression.

下面是不能使用 Binding 标记扩展/{Binding} 表达式格式设置的 Binding 属性。The following are properties of Binding that cannot be set using the Binding markup extension/{Binding} expression form.

  • UpdateSourceExceptionFilter:此属性需要对回调实现的引用。UpdateSourceExceptionFilter: this property expects a reference to a callback implementation. 不能在 XAML 语法中引用事件处理程序以外的回调/方法。Callbacks/methods other than event handlers cannot be referenced in XAML syntax.

  • ValidationRules:属性采用 ValidationRule 对象的泛型集合。ValidationRules: the property takes a generic collection of ValidationRule objects. 这可以表示为 Binding 对象元素中的属性元素,但没有任何可用于 Binding 表达式中的用法的属性分析技术。This could be expressed as a property element in a Binding object element, but has no readily available attribute-parsing technique for usage in a Binding expression. 有关 ValidationRules,请参阅参考主题。See reference topic for ValidationRules.

  • XmlNamespaceManager

备注Remarks

重要

就依赖属性优先级而言,Binding 表达式等效于本地设置的值。In terms of dependency property precedence, a Binding expression is equivalent to a locally set value. 如果为先前具有 Binding 表达式的属性设置本地值,则 Binding 将被完全删除。If you set a local value for a property that previously had a Binding expression, the Binding is completely removed. 有关详细信息,请参阅依赖属性值优先级For details, see Dependency Property Value Precedence.

本主题不涉及在基本级别描述数据绑定。Describing data binding at a basic level is not covered in this topic. 请参阅数据绑定概述See Data Binding Overview.

备注

MultiBindingPriorityBinding 不支持 XAMLXAML 扩展语法。MultiBinding and PriorityBinding do not support a XAMLXAML extension syntax. 您将改用属性元素。You would instead use property elements. 有关 MultiBindingPriorityBinding,请参阅参考主题。See reference topics for MultiBinding and PriorityBinding.

XAML 的布尔值不区分大小写。Boolean values for XAML are case insensitive. 例如,可以指定 {Binding NotifyOnValidationError=true}{Binding NotifyOnValidationError=True}For example you could specify either {Binding NotifyOnValidationError=true} or {Binding NotifyOnValidationError=True}.

涉及数据验证的绑定通常由显式 Binding 元素指定,而不是作为 {Binding ...} 表达式指定,在表达式中设置 ValidatesOnDataErrorsValidatesOnExceptions 的情况并不常见。Bindings that involve data validation are typically specified by an explicit Binding element rather than as a {Binding ...} expression, and setting ValidatesOnDataErrors or ValidatesOnExceptions in an expression is uncommon. 这是因为无法在表达式窗体中轻松设置伴生属性 ValidationRulesThis is because the companion property ValidationRules cannot be readily set in the expression form. 有关详细信息,请参阅实现绑定验证For more information, see Implement Binding Validation.

Binding 是标记扩展。Binding 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 type converters attributed 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 string contents. 有关详细信息,请参阅标记扩展和 WPF XAMLFor more information, see Markup Extensions and WPF XAML.

Binding 是一种典型的标记扩展,因为实现 WPF XAML 实现扩展功能的 Binding 类还实现了与 XAML 无关的多个其他方法和属性。Binding is an atypical markup extension in that the Binding class that implements the extension functionality for WPF's XAML implementation also implements several other methods and properties that are not related to XAML. 其他成员旨在使 Binding 一个更通用的自包含类,除了作为 XAML 标记扩展外,还可以处理许多数据绑定方案。The other members are intended to make Binding a more versatile and self-contained class that can address many data binding scenarios in addition to functioning as a XAML markup extension.

请参阅See also