共用方式為


PropertyPath XAML Syntax

The PropertyPath object supports a complex XAML syntax when setting various properties that take the PropertyPath type as their value.

The various syntaxes are listed here by their forms, going from simplest to most complex.

XAML Syntaxes

Single Property, Current Namescope

<object property="propertyName" .../>

propertyName must resolve to be the name of a property that is in the property namescope. "Property namescope" is defined as one of the following, depending on the property path type being set:

Multiple Property (Indirect Property Targeting)

<object property="propertyName.propertyName2" .../>

propertyName must resolve to be the name of a property that is in the property namescope. "Property namescope" is defined as one of the following:

  • The current DataContext for a System.Windows.Data.Binding.Path usage. The property that is the target of the binding must be a DependencyProperty, but the path property propertyName and propertyName2 can be any property that exist in a relationship.

  • The type of the specified System.Windows.Media.Animation.Storyboard.TargetName object in a System.Windows.Media.Animation.Storyboard.TargetProperty usage. For storyboards, the propertyName and propertyName2 property must be a DependencyProperty. propertyName2 must resolve to be the Name of a DependencyProperty that is registered to or inherited by the Type of propertyName.

Single Property, Out of Namescope, Runtime Reflection

<object property="ownerType.propertyName" .../>

propertyName must resolve to be the name of a property existing on the ownerType. This need not be a DependencyProperty. ownerType is expected to be some type that can be found based on runtime reflection. As such, it is not legal to qualify with an xmlns prefix, because such a prefix has no meaning to reflection, only to compile time XAML. See next section.

Single Property, Attached or Out of Namescope, Parser Context

<object property="(ownerType.propertyName)" .../>

The parentheses indicate that this PropertyPath should be constructed based on the parser context, and thus uses xmlns namespace considerations. propertyName must resolve to be the name of a property existing on the ownerType. The ownerType.propertyName combination might also reference an attached property. ownerType is expected to be some other type accessible in the overall default WPF xmlns namespace, or can also be qualified by an xmlns prefix (see XAML Namespaces and Namespace Mapping).

For use as storyboard target, the property must be a DependencyProperty.

Index of a Collection Property, Simplest Form

<object property="propertyName[index]" .../>

propertyName must resolve to current property namescope in this simplest example. index is the index into the enumerable collection or array that is the value of that property.

Source Traversal (Binding Specific)

<object Path="propertyName/propertyNameX" .../>

The / in this syntax is used to traverse within a hierarchical data source object, and supports multiple steps into the hierarchy with successive / characters. For details on binding with hierarchical data source objects, see How to: Use the Master-Detail Pattern with Hierarchical Data.

NoteNote:

Superficially, this syntax resembles XPath but a true XPath is not used as a Path value and should instead be used for the mutually exclusive XPath property.

Remarks

Prominent properties that take a PropertyPath include System.Windows.Data.Binding.Path, and System.Windows.Media.Animation.Storyboard.TargetProperty. PropertyPath enables a "drill-down" syntax that can describe a property by relationships in string form without requiring specific objects such as Type, a TypeDescriptor, or DependencyProperty identifiers. The syntax is capable of indexing into collections or arrays, of specifying attached properties or properties that are out of current scope, and of specifying a path of a binding into the object model of a data source.

Some style and template properties such as Setter.Property take a qualified property name that superficially resembles a PropertyPath. But this is not a true PropertyPath, instead it is a qualified usage that is enabled by the XAML reader in combination with the type converter for DependencyProperty.

See Also

Reference

PropertyPath

Concepts

Data Binding Overview
Storyboards Overview