RelativeSource.AncestorLevel Property

Definition

Gets or sets the level of ancestor to look for, in FindAncestor mode. Use 1 to indicate the one nearest to the binding target element.

C#
public int AncestorLevel { get; set; }

Property Value

The ancestor level. Use 1 to indicate the one nearest to the binding target element.

Examples

The following returns the second ItemsControl encountered on the upward path starting at the target element of the binding.

C#
Binding myBinding = new Binding();
// Returns the second ItemsControl encountered on the upward path
// starting at the target element of the binding
myBinding.RelativeSource = new RelativeSource(
    RelativeSourceMode.FindAncestor, typeof(ItemsControl), 2);

Remarks

If the Mode property is not set explicitly, setting the AncestorType or the AncestorLevel property will implicitly lock the Mode property value to FindAncestor.

XAML Text Usage

For XAML information, see RelativeSource MarkupExtension.

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also