Silverlight
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 at run time.

XAML Attribute Usage

<object property="{Binding}" .../>

-or-

<object property="{Binding propertyPath}" .../>

-or-

<object property="{Binding oneOrMoreBindingProperties}" .../>

-or-

<object property="{Binding propertyPath, oneOrMoreBindingProperties}" .../>
XAML Values

propertyPath

A string that specifies the property path for the binding. See "Property Paths" section.

oneOrMoreBindingProperties

bindingPropertyName=value[bindingPropertyName=value]*

The []* in the syntax are not literals. They are indicators that more than one name/value pair is possible.

Each name/value pair should be separated from other name/value pairs. The separator can be either a comma or a space.

bindingPropertyName

The string name of the Binding property to set. Some properties are settable only by using further nested markup extensions. See "Binding Properties That Can Be Set with the Binding Extension" section.

value

The value to set the property to. The handling of the attribute value is ultimately specific to the type and logic of the bindingPropertyName being set.

Property Paths

The Path of a binding is conceptually similar to its "default property" in the Binding markup extension usage. You can establish the Path either through the string immediately after the {Binding initial part of the Binding markup extension, or through the Binding constructor in code. Optionally, you can also specify Path= explicitly as a named argument in the Binding markup extension usage.

The Path for a binding is a property path. A property path is a string that is evaluated as instructions to search an object for a property of that name and return the value. The property path can be used to traverse into the object model of a CLR object or DependencyObject in cases where the value of the initially named property is an object, and the value you want is from a subproperty of that object.

The delimiter between steps in a property path is a dot (.). You can include multiple delimiters to reference successive subproperties until the desired property value is reached.

For example, you might bind to a business object that represents an employee. If you wanted to bind UI to the string for the employee's first name, your data binding path might be Employee.FirstName. Or, if you were binding an items control to a collection property that lists the employee's dependents, your data binding path might be Employee.Dependents, and a data template applied to the items control would take care of displaying individual properties of the Dependents items.

If the data source is a collection, then the property path specifies properties to be found on a collection item object.

When using ElementName binding to an existing DependencyObject, you can use attached properties as part of the property path. To disambiguate attached property usages so that the intermediate dot in the attached property name is not considered a step into a path, put parentheses around the owner-qualified attached property name; for example, (Validation.Errors).

For more information about the string syntax for a property path, as well as parallel usages for property paths in animation feature areas and for constructing a PropertyPath object, see Property Path Syntax.

NoteNote:

If you are familiar with WPF data binding, you know that WPF supports property path techniques for retrieving indexed items in a collection and for retrieving attached property values. Silverlight 3 supports these techniques, but with limitations. See Property Path Syntax.

Binding Properties That Can Be Set with the Binding Extension

The syntax shown in this topic uses the grammar bindingPropertyName=value because there are multiple read/write properties of Binding that can be set through the Binding markup extension syntax. They can be set in any order. Basically, you can set zero or more of the properties named in the following list, in any order, using bindingPropertyName=value pairs separated by commas.

Several of these property values require object types that do not support a native type conversion, and thus require further markup extension usages to be set in XAML, nested within the Binding markup extension:

  • Path. As noted in the preceding "Property Paths" section, you can establish the Path through the string immediately after the {Binding initial part of the Binding markup extension, or you can also specify Path= explicitly in the usage just like the other named Binding properties.

  • Converter. Can be set in XAML, but only if you are referring to a converter that is defined in such a way that it can be instantiated and placed in a ResourceDictionary in XAML. The XAML reference then requires a StaticResource reference to that object in the resource dictionary.

  • ConverterCulture. Settable as a standards-based identifier; see the reference for ConverterCulture.

  • ConverterParameter. If passing a reference type, can be set in XAML, but only if you are referring to an object value that is defined in such a way that it can be instantiated and placed in a ResourceDictionary in XAML. The XAML reference then requires a StaticResource reference to that object in the resource dictionary. If passing a primitive (such as a string or integer), you can use built-in parsing behavior and simply pass the value. But for nonprimitives, the TypeConverter pathway for obtaining an object from a string is not available because there is no way to identify which object you want to convert to based on the Object type of ConverterParameter.

  • Mode. Specifies the binding mode, as one of the following strings: OneTime, OneWay, TwoWay.

  • Source. Specifies the object data source. For usage within the Binding markup extension, requires an object reference such as a StaticResource reference. If this is left unspecified, the acting data context specifies the source.

  • NotifyOnValidationError. Enables a validation/notification mode on the binding. Can be true or false (default is false).

  • ValidatesOnExceptions. Enables a validation/notification mode on the binding. Can be true or false (default is false).

  • ElementName. Specifies a data source by referencing another element that has a Name property or x:Name Attribute (the ElementName usage specifies that name). The element must exist in the same XAML namescope.

  • RelativeSource. Specifies a data source by describing the position of the binding source relative to the position of the binding target. This is expressed in terms of the run-time object graph, and can be Self or TemplatedParent. Whether set in attribute form or as a binding property in the expression, setting RelativeSource in XAML requires using the RelativeSource Markup Extension.

  • UpdateSourceTrigger. Determines the timing of binding source updates, as one of the following strings: Default, Explicit. The default if unspecified is the Default update mode.

Remarks

Important noteImportant Note:

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.

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

Three of the possible binding properties in the Binding markup extension syntax (Source, RelativeSource, and ElementName) specify a binding source. These properties are mutually exclusive in a binding. If you have set one of these properties, then setting either of the other two in a binding (through XAML as per this markup extension or through code) causes an exception when the binding expression is applied to a property.

TipTip:

You can specify opening or closing braces ({ }) for values such as Path or ConverterParameter by enclosing the value in the secondary quotation set. For instance, if the Binding itself uses double quotation marks ("), enclose the string that contains the brace ({) in single quotation marks(').

Some aspects of the Binding usage in Silverlight are subsets of the Binding markup extension usage available in WPF. Notably, WPF supports BindDirectlyToSource, FallbackValue, XPath, NotifyOnTargetUpdated, and NotifyOnSourceUpdated, and these binding characteristics are reflected in the WPF Binding class; Silverlight 3 does not support these properties in its Binding class. Also, WPF has more possible values for UpdateSourceTrigger, Mode, and RelativeSource

See Also

Reference

Concepts

Tags :


Community Content

Synergist
Using { and } in the ConverterParameter

If you need to use the { or } characters in the ConverterParameter property, use single quotes around the value like this:

ConverterParameter='{0:C}'


Wolf Schmidt - MSFT
Re {
Good point I will add it to the topic. This also made me realize that the escaping mechanism you are supposed to use for a { in general XAML, which is to proceed it with {} a la {}{, does not work if you are ALREADY in a markup extension such as Binding. Apparently the escape doesn't know how to unescape; reinvoking it does not do so, and you need to unescape to complete the outer extension.
Tags :

Page view tracker