Partager via


RelativeSource MarkupExtension

Specifies properties of a RelativeSource binding source, to be used within a Binding Markup Extension, or when setting the RelativeSource property of a Binding element established in XAML.

XAML Attribute Usage

<Binding RelativeSource="{RelativeSource modeEnumValue}" .../>

XAML Attribute Usage (nested within Binding extension)

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

XAML Object Element Usage

<Binding>
  <Binding.RelativeSource>
    <RelativeSource Mode="modeEnumValue"/>
  </Binding.RelativeSource>
</Binding>

- or

<Binding>
  <Binding.RelativeSource>
    <RelativeSource
      Mode="FindAncestor"
      AncestorType="{x:Type typeName}"
      AncestorLevel="intLevel"
    />
  </Binding.RelativeSource>
</Binding>

XAML Values

modeEnumValue

One of the following:

  • The string token Self; corresponds to a RelativeSource as created with its Mode property set to Self.

  • The string token TemplatedParent; corresponds to a RelativeSource as created with its Mode property set to TemplatedParent.

  • The string token PreviousData; corresponds to a RelativeSource as created with its Mode property set to PreviousData.

  • See below for information on FindAncestor mode.

FindAncestor

The string token FindAncestor. Using this token enters a mode whereby a RelativeSource specifies an ancestor type and optionally an ancestor level. This corresponds to a RelativeSource as created with its Mode property set to FindAncestor.

typeName

Required for FindAncestor mode. Name of a type, which fills the AncestorType property.

intLevel

Optional for FindAncestor mode. An ancestor level (evaluated towards the parent direction in the logical tree).

Remarks

In the object element syntax for FindAncestor mode shown above, the second object element syntax is used specifically for FindAncestor mode. FindAncestor mode requires an AncestorType value. You must set AncestorType as an attribute using an x:Type Markup Extension reference to the type of ancestor to look for. The AncestorType value is used when the binding request is processed at run-time.

For FindAncestor mode, the optional property AncestorLevel can help disambiguate the ancestor lookup in cases where there is possibly more than one ancestor of that type existing in the element tree.

For more details on using the FindAncestor mode, see RelativeSource

Describing data binding as a concept is not covered here, see Data Binding Overview.

In the WPF XAML reader implementation, the handling for this markup extension is defined by the TypeExtension class.

RelativeSource 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 reader recognizes that a markup extension must process the attribute. For more information, see Markup Extensions and XAML.

See Also

Reference

x:Type Markup Extension
Binding

Concepts

Styling and Templating
XAML Overview
Markup Extensions and XAML
Data Binding Overview
Binding Declarations Overview