Table of Contents

Class VisualExtensions

Namespace
Avalonia.VisualTree
Assembly
Avalonia.Base.dll

Provides extension methods for working with the visual tree.

public static class VisualExtensions
Inheritance
VisualExtensions
Inherited Members

Methods

CalculateDistanceFromAncestor(Visual, Visual?)

Calculates the distance from a visual's ancestor.

public static int CalculateDistanceFromAncestor(this Visual visual, Visual? ancestor)

Parameters

visual Visual

The visual.

ancestor Visual

The ancestor visual.

Returns

int

The number of steps from the visual to the ancestor or -1 if visual is not a descendent of ancestor.

CalculateDistanceFromRoot(Visual)

Calculates the distance from a visual's root.

public static int CalculateDistanceFromRoot(Visual visual)

Parameters

visual Visual

The visual.

Returns

int

The number of steps from the visual to the root.

FindAncestorOfType<T>(Visual?, bool)

Finds first ancestor of given type.

public static T? FindAncestorOfType<T>(this Visual? visual, bool includeSelf = false) where T : class

Parameters

visual Visual

The visual.

includeSelf bool

If given visual should be included in search.

Returns

T

First ancestor of given type.

Type Parameters

T

Ancestor type.

FindCommonVisualAncestor(Visual?, Visual?)

Tries to get the first common ancestor of two visuals.

public static Visual? FindCommonVisualAncestor(this Visual? visual, Visual? target)

Parameters

visual Visual

The first visual.

target Visual

The second visual.

Returns

Visual

The common ancestor, or null if not found.

FindDescendantOfType<T>(Visual?, bool)

Finds first descendant of given type.

public static T? FindDescendantOfType<T>(this Visual? visual, bool includeSelf = false) where T : class

Parameters

visual Visual

The visual.

includeSelf bool

If given visual should be included in search.

Returns

T

First descendant of given type.

Type Parameters

T

Descendant type.

GetSelfAndVisualAncestors(Visual)

Enumerates an Visual and its ancestors in the visual tree.

public static IEnumerable<Visual> GetSelfAndVisualAncestors(this Visual visual)

Parameters

visual Visual

The visual.

Returns

IEnumerable<Visual>

The visual and its ancestors.

GetSelfAndVisualDescendants(Visual)

Enumerates an Visual and its descendants in the visual tree.

public static IEnumerable<Visual> GetSelfAndVisualDescendants(this Visual visual)

Parameters

visual Visual

The visual.

Returns

IEnumerable<Visual>

The visual and its ancestors.

GetTransformedBounds(Visual)

public static TransformedBounds? GetTransformedBounds(this Visual visual)

Parameters

visual Visual

Returns

TransformedBounds?

GetVisualAncestors(Visual)

Enumerates the ancestors of an Visual in the visual tree.

public static IEnumerable<Visual> GetVisualAncestors(this Visual visual)

Parameters

visual Visual

The visual.

Returns

IEnumerable<Visual>

The visual's ancestors.

GetVisualAt(Visual, Point)

Gets the first visual in the visual tree whose bounds contain a point.

public static Visual? GetVisualAt(this Visual visual, Point p)

Parameters

visual Visual

The root visual to test.

p Point

The point.

Returns

Visual

The visual at the requested point.

GetVisualAt(Visual, Point, Func<Visual, bool>)

Gets the first visual in the visual tree whose bounds contain a point.

public static Visual? GetVisualAt(this Visual visual, Point p, Func<Visual, bool> filter)

Parameters

visual Visual

The root visual to test.

p Point

The point.

filter Func<Visual, bool>

A filter predicate. If the predicate returns false then the visual and all its children will be excluded from the results.

Returns

Visual

The visual at the requested point.

GetVisualChildren(Visual)

Enumerates the children of an Visual in the visual tree.

public static IEnumerable<Visual> GetVisualChildren(this Visual visual)

Parameters

visual Visual

The visual.

Returns

IEnumerable<Visual>

The visual children.

GetVisualDescendants(Visual)

Enumerates the descendants of an Visual in the visual tree.

public static IEnumerable<Visual> GetVisualDescendants(this Visual visual)

Parameters

visual Visual

The visual.

Returns

IEnumerable<Visual>

The visual's ancestors.

GetVisualParent(Visual)

Gets the visual parent of an Visual.

public static Visual? GetVisualParent(this Visual visual)

Parameters

visual Visual

The visual.

Returns

Visual

The parent, or null if the visual is unparented.

GetVisualParent<T>(Visual)

Gets the visual parent of an Visual.

public static T? GetVisualParent<T>(this Visual visual) where T : class

Parameters

visual Visual

The visual.

Returns

T

The parent, or null if the visual is unparented or its parent is not of type T.

Type Parameters

T

The type of the visual parent.

GetVisualRoot(Visual)

Gets the root visual for an Visual.

public static IRenderRoot? GetVisualRoot(this Visual visual)

Parameters

visual Visual

The visual.

Returns

IRenderRoot

The root visual or null if the visual is not rooted.

GetVisualsAt(Visual, Point)

Enumerates the visible visuals in the visual tree whose bounds contain a point.

public static IEnumerable<Visual> GetVisualsAt(this Visual visual, Point p)

Parameters

visual Visual

The root visual to test.

p Point

The point.

Returns

IEnumerable<Visual>

The visuals at the requested point.

GetVisualsAt(Visual, Point, Func<Visual, bool>)

Enumerates the visuals in the visual tree whose bounds contain a point.

public static IEnumerable<Visual> GetVisualsAt(this Visual visual, Point p, Func<Visual, bool> filter)

Parameters

visual Visual

The root visual to test.

p Point

The point.

filter Func<Visual, bool>

A filter predicate. If the predicate returns false then the visual and all its children will be excluded from the results.

Returns

IEnumerable<Visual>

The visuals at the requested point.

IsAttachedToVisualTree(Visual)

Returns a value indicating whether this control is attached to a visual root.

public static bool IsAttachedToVisualTree(this Visual visual)

Parameters

visual Visual

Returns

bool

IsVisualAncestorOf(Visual?, Visual?)

Tests whether an Visual is an ancestor of another visual.

public static bool IsVisualAncestorOf(this Visual? visual, Visual? target)

Parameters

visual Visual

The visual.

target Visual

The potential descendant.

Returns

bool

True if visual is an ancestor of target; otherwise false.

SortByZIndex(IEnumerable<Visual>)

public static IEnumerable<Visual> SortByZIndex(this IEnumerable<Visual> elements)

Parameters

elements IEnumerable<Visual>

Returns

IEnumerable<Visual>