In This Article

DependencyObjectExtensions Class

Provides extension methods for the type System.Windows.DependencyObject

public static class DependencyObjectExtensions
Inheritance:
object object

Methods

FindAncestorOfType<T>(DependencyObject?, bool)

Finds the first ancestor of the given type in the visual tree.

public static T? FindAncestorOfType<T>(this DependencyObject? visual, bool includeSelf = false) where T : class
Type Parameters:
T -

The type of ancestor to find.

Parameter Type Description
visual DependencyObject

The visual to examine.

includeSelf bool

Indicates if the given visual should be included in the search.

Returns

T:

The first ancestor of the given type, or null if a match was not found.

FindCommonVisualAncestor(DependencyObject?, DependencyObject?)

Tries to find the closest ancestor in the visual tree that is common between two visuals.

public static DependencyObject? FindCommonVisualAncestor(this DependencyObject? visual, DependencyObject? target)
Parameter Type Description
visual DependencyObject

The first visual to examine.

target DependencyObject

The second visual to examine.

Returns

DependencyObject:

The closest ancestor in the visual tree that is common between two visuals.

FindDescendantOfType<T>(DependencyObject?, bool)

Finds the first descendant of the given type in the visual tree.

public static T? FindDescendantOfType<T>(this DependencyObject? visual, bool includeSelf = false) where T : class
Type Parameters:
T -

The type of descendant to find.

Parameter Type Description
visual DependencyObject

The visual to examine.

includeSelf bool

Indicates if the given visual should be included in the search.

Returns

T:

The first descendant of the given type, or null if a match was not found.

FindLogicalAncestorOfType<T>(DependencyObject?, bool)

Finds the first ancestor of the given type in the logical tree.

public static T? FindLogicalAncestorOfType<T>(this DependencyObject? logical, bool includeSelf = false) where T : class
Type Parameters:
T -

The type of ancestor to find.

Parameter Type Description
logical DependencyObject

The object to examine.

includeSelf bool

Indicates if the given object should be included in the search.

Returns

T:

The first ancestor of the given type, or null if a match was not found.

FindLogicalDescendantOfType<T>(DependencyObject?, bool)

Finds the first descendant of the given type in the logical tree.

public static T? FindLogicalDescendantOfType<T>(this DependencyObject? logical, bool includeSelf = false) where T : class
Type Parameters:
T -

The type of descendant to find.

Parameter Type Description
logical DependencyObject

The object to examine.

includeSelf bool

Indicates if the given object should be included in the search.

Returns

T:

The first descendant of the given type, or null if a match was not found.

GetLogicalAncestors(DependencyObject)

Enumerates the ancestors of an object in the logical tree.

public static IEnumerable<DependencyObject> GetLogicalAncestors(this DependencyObject logical)
Parameter Type Description
logical DependencyObject

The object to examine.

Returns

IEnumerable<DependencyObject>

GetLogicalChildren(DependencyObject)

Enumerates the children of an object in the logical tree.

public static IEnumerable<DependencyObject> GetLogicalChildren(this DependencyObject logical)
Parameter Type Description
logical DependencyObject

The object to examine.

Returns

IEnumerable<DependencyObject>

GetLogicalDescendants(DependencyObject)

Enumerates the descendants of an object in the logical tree.

public static IEnumerable<DependencyObject> GetLogicalDescendants(this DependencyObject logical)
Parameter Type Description
logical DependencyObject

The object to examine.

Returns

IEnumerable<DependencyObject>

GetLogicalParent(DependencyObject)

Gets the parent of an object in the logical tree.

public static DependencyObject? GetLogicalParent(this DependencyObject logical)
Parameter Type Description
logical DependencyObject

The object to examine.

Returns

DependencyObject:

The parent, or null if the object does not have a parent.

GetLogicalParent<T>(DependencyObject)

Gets the parent of an object in the logical tree.

public static T? GetLogicalParent<T>(this DependencyObject logical) where T : class
Type Parameters:
T -

The type of the logical parent.

Parameter Type Description
logical DependencyObject

The object to examine.

Returns

T:

The parent, or null if the object does not have a parent or its parent is not of the type T.

GetLogicalSiblings(DependencyObject)

Enumerates the siblings of an object in the logical tree.

public static IEnumerable<DependencyObject> GetLogicalSiblings(this DependencyObject logical)
Parameter Type Description
logical DependencyObject

The object to examine.

Returns

IEnumerable<DependencyObject>

GetSelfAndLogicalAncestors(DependencyObject)

Enumerates an object and its ancestors in the logical tree.

public static IEnumerable<DependencyObject> GetSelfAndLogicalAncestors(this DependencyObject logical)
Parameter Type Description
logical DependencyObject

The object to examine.

Returns

IEnumerable<DependencyObject>

GetSelfAndLogicalDescendants(DependencyObject)

Enumerates an object and its descendants in the logical tree.

public static IEnumerable<DependencyObject> GetSelfAndLogicalDescendants(this DependencyObject logical)
Parameter Type Description
logical DependencyObject

The object to examine.

Returns

IEnumerable<DependencyObject>

GetSelfAndVisualAncestors(DependencyObject)

Enumerates a visual and its ancestors in the visual tree.

public static IEnumerable<DependencyObject> GetSelfAndVisualAncestors(this DependencyObject visual)
Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

IEnumerable<DependencyObject>

GetSelfAndVisualDescendants(DependencyObject)

Enumerates a visual and its descendants in the visual tree.

public static IEnumerable<DependencyObject> GetSelfAndVisualDescendants(this DependencyObject visual)
Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

IEnumerable<DependencyObject>

GetVisualAncestors(DependencyObject)

Enumerates the ancestors of a visual in the visual tree.

public static IEnumerable<DependencyObject> GetVisualAncestors(this DependencyObject visual)
Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

IEnumerable<DependencyObject>

GetVisualChildren(DependencyObject)

Enumerates the children of a visual in the visual tree.

public static IEnumerable<DependencyObject> GetVisualChildren(this DependencyObject visual)
Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

IEnumerable<DependencyObject>

GetVisualDescendants(DependencyObject)

Enumerates the descendants of a visual in the visual tree.

public static IEnumerable<DependencyObject> GetVisualDescendants(this DependencyObject visual)
Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

IEnumerable<DependencyObject>

GetVisualParent(DependencyObject)

Gets the parent of a visual in the visual tree.

public static DependencyObject? GetVisualParent(this DependencyObject visual)
Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

DependencyObject:

The parent, or null if the visual does not have a parent.

GetVisualParent<T>(DependencyObject)

Gets the parent of a visual in the visual tree.

public static T? GetVisualParent<T>(this DependencyObject visual) where T : class
Type Parameters:
T -

The type of the visual parent.

Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

T:

The parent, or null if the visual does not have a parent or its parent is not of the type T.

GetVisualRoot(DependencyObject)

Gets the root visual for a visual.

public static DependencyObject? GetVisualRoot(this DependencyObject visual)
Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

DependencyObject:

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

GetVisualSiblings(DependencyObject)

Enumerates the siblings of an object in the visual tree.

public static IEnumerable<DependencyObject> GetVisualSiblings(this DependencyObject visual)
Parameter Type Description
visual DependencyObject

The visual to examine.

Returns

IEnumerable<DependencyObject>

IsLogicalAncestorOf(DependencyObject?, DependencyObject?)

Tests whether an object is an ancestor of another object in the logical tree.

public static bool IsLogicalAncestorOf(this DependencyObject? logical, DependencyObject? target)
Parameter Type Description
logical DependencyObject

The object to examine.

target DependencyObject

The potential descendant.

Returns

bool:

true if the object is an ancestor of target; otherwise, false.

IsVisual(DependencyObject?)

Tests whether the specified System.Windows.DependencyObject is a Visual or Visual3D.

public static bool IsVisual(this DependencyObject? obj)
Parameter Type Description
obj DependencyObject

The System.Windows.DependencyObject to examine.

Returns

bool:

true if the specified System.Windows.DependencyObject is a Visual or Visual3D; otherwise, false.

IsVisualAncestorOf(DependencyObject?, DependencyObject?)

Tests whether a visual is an ancestor of another visual in the visual tree.

public static bool IsVisualAncestorOf(this DependencyObject? visual, DependencyObject? target)
Parameter Type Description
visual DependencyObject

The visual to examine.

target DependencyObject

The potential descendant.

Returns

bool:

true if the visual is an ancestor of target; otherwise, false.

IsWithin(DependencyObject?, DependencyObject?, bool, bool)

Tests whether an object is within the hierarchical tree of a possible ancestor object.

public static bool IsWithin(this DependencyObject? possibleDescendant, DependencyObject? possibleAncestor, bool includeSelf = false, bool mergePopups = false)
Parameter Type Description
possibleDescendant DependencyObject

The object to be tested.

possibleAncestor DependencyObject

The possible ancestor.

includeSelf bool

true if the object is allowed to be "within" itself.

mergePopups bool

true if a popup in the hierarchy should be merged into the hierarchy of either its logical parent or placement target.

Returns

bool:

true if the object is not null and within the hierarchy of the ancestor; otherwise, false.

Remarks

This method primarily searches the visual tree but can optionally merge a popup into the hierarchy of either its logical parent or placement target. Merging the popup is particularly useful when checking if keyboard focus or pointer capture are conceptually "within" a root object even though they are in separate visual trees.

IsWithin(FrameworkElement?, DependencyObject?, bool, bool)

Tests whether a framework element is within the hierarchical tree of a possible ancestor object.

public static bool IsWithin(this FrameworkElement? possibleDescendant, DependencyObject? possibleAncestor, bool includeSelf = false, bool mergePopups = false)
Parameter Type Description
possibleDescendant FrameworkElement

The framework element to be tested.

possibleAncestor DependencyObject

The possible ancestor.

includeSelf bool

true if the object is allowed to be "within" itself.

mergePopups bool

true if a popup in the hierarchy should be merged into the hierarchy of either its logical parent or placement target.

Returns

bool:

true if the object is not null and within the hierarchy of the ancestor; otherwise, false.

Remarks

This method primarily searches the visual tree but can optionally merge a popup into the hierarchy of either its logical parent or placement target. Merging the popup is particularly useful when checking if keyboard focus or pointer capture are conceptually "within" a root object even though they are in separate visual trees.

IsWithin(IInputElement?, DependencyObject?, bool, bool)

Tests whether an input element is a dependency object within the hierarchical tree of a possible ancestor object.

public static bool IsWithin(this IInputElement? possibleDescendant, DependencyObject? possibleAncestor, bool includeSelf = false, bool mergePopups = false)
Parameter Type Description
possibleDescendant IInputElement

The input element to be tested.

possibleAncestor DependencyObject

The possible ancestor.

includeSelf bool

true if the object is allowed to be "within" itself.

mergePopups bool

true if a popup in the hierarchy should be merged into the hierarchy of either its logical parent or placement target.

Returns

bool:

true if the object is not null and within the hierarchy of the ancestor; otherwise, false.

Remarks

This method primarily searches the visual tree but can optionally merge a popup into the hierarchy of either its logical parent or placement target. Merging the popup is particularly useful when checking if keyboard focus or pointer capture are conceptually "within" a root object even though they are in separate visual trees.

Inherited Members