Редагувати

Поділитися через


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. 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.

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. 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. 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. 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 Values

Value Description
bindProp1, bindPropN The name of the Binding or BindingBase property to set. 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. The handling of the attribute value is ultimately specific to the type and logic of the specific Binding property being set.
path The path string that sets the implicit Binding.Path property. See also PropertyPath XAML Syntax.

Unqualified {Binding}

The {Binding} usage shown in "Binding Expression Usage" creates a Binding object with default values, which includes an initial Binding.Path of null. This 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

The Binding markup extension uses Binding.Path as a conceptual "default property", where Path= does not need to appear in the expression. 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. 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. 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

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. They can be set in any order, with the exception of an implicit Binding.Path. (You do have the option to explicitly specify Path=, in which case it can be set in any order). Basically, you can set zero or more of the properties in the list below, using bindProp=value pairs separated by commas.

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. 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.

The following are properties of Binding that cannot be set using the Binding markup extension/{Binding} expression form.

  • UpdateSourceExceptionFilter: this property expects a reference to a callback implementation. Callbacks/methods other than event handlers cannot be referenced in XAML syntax.

  • ValidationRules: the property takes a generic collection of ValidationRule objects. 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. See reference topic for ValidationRules.

  • XmlNamespaceManager

Remarks

Important

In terms of dependency property precedence, a Binding expression is equivalent to a locally set value. 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.

Note

MultiBinding and PriorityBinding do not support a XAML extension syntax. You would instead use property elements. See reference topics for MultiBinding and PriorityBinding.

Boolean values for XAML are case insensitive. For example you could specify either {Binding NotifyOnValidationError=true} or {Binding NotifyOnValidationError=True}.

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. This is because the companion property ValidationRules cannot be readily set in the expression form. For more information, see Implement Binding Validation.

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. 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. For more information, see Markup Extensions and WPF 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. 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