Skip to content

VisualTreeHelper.FindElementsInHostCoordinates returns each descendant twice, in document order instead of front-most first #25267

Open

Description

@MartinZikmund

Current behavior 🐛

VisualTreeHelper.FindElementsInHostCoordinates(Point, UIElement) (and the includeAllElements overload) returns every intersecting descendant of subtree twice, and returns them in document (pre-order) order, parent first and back-most sibling first, instead of front-most first.

For Grid > [Border "back", Border "front"], hit-testing the center gives:

Grid, back, back, front, front

Cause: for each child, FindElementsInHostCoordinates does yield return uiElement, and then recurses into FindElementsInHostCoordinates(point, uiElement, ...). The first thing the recursive call does is yield its own subtree (the same child) again. The walk is a plain pre-order traversal over GetChildren() with no z-order or reverse-render ordering.

Code (commit cc48541):

public static IEnumerable<UIElement> FindElementsInHostCoordinates(Point intersectingPoint, UIElement/* ? */ subtree, bool includeAllElements)
{
if (subtree != null)
{
if (IsElementIntersecting(intersectingPoint, subtree))
{
yield return subtree;
}
foreach (var child in subtree.GetChildren())
{
var uiElement = child;
var canTest = includeAllElements
|| (uiElement.IsHitTestVisible && uiElement.IsViewHit());
if (canTest)
{
if (IsElementIntersecting(intersectingPoint, uiElement))
{
yield return uiElement;
}
foreach (var subChild in FindElementsInHostCoordinates(intersectingPoint, uiElement, includeAllElements))
{
yield return subChild;
}
}
}
}
}

Expected behavior 🎯

Each element is returned once, front-most (topmost z-order) first, like WinUI.

Backed by:

  • Docs for VisualTreeHelper.FindElementsInHostCoordinates: the method returns "the whole stack of elements that share that point", "listed by visual tree order (which is usually the same as inverse of XAML declaration order)", and the order "accounts for both coordinate space in the area and for the z-order".
  • WinUI C++: VisualTreeHelper::FindElementsInHostCoordinatesPointStatic in dxaml/xcp/dxaml/lib/VisualTreeHelper.cpp delegates to the core hit test (CoreImports::UIElement_HitTestPoint), the same walk used for real input, which yields a list of hit elements, not a per-child pre-order concatenation.

I did not run the repro natively on WinUI; the WinUI claim is docs- and source-backed.

How to reproduce it (as minimally and precisely as possible) 🔬

  1. Add this runtime test to src/Uno.UI.RuntimeTests/Microsoft/UI/Xaml/Media/Given_VisualTreeHelper.cs:
[TestMethod]
public async Task When_FindElementsInHostCoordinates_OverlappingBorders()
{
    Border back = new() { Name = "back", Background = new SolidColorBrush(Colors.Red) };
    Border front = new() { Name = "front", Background = new SolidColorBrush(Colors.Blue) };
    Grid root = new() { Width = 100, Height = 100, Background = new SolidColorBrush(Colors.White) };
    root.Children.Add(back);
    root.Children.Add(front);
    await UITestHelper.Load(root);

    var center = root.GetAbsoluteBounds().GetCenter();
    var names = VisualTreeHelper.FindElementsInHostCoordinates(center, root)
        .Select(e => (e as FrameworkElement)?.Name ?? e.GetType().Name)
        .ToArray();
    Console.WriteLine("RESULT: " + string.Join(", ", names));

    // WinUI: each element once, front-most first
    CollectionAssert.AreEqual(new[] { "front", "back", "Grid" }, names);
}
  1. Run it on Skia Desktop with UITEST_RUNTIME_TESTS_FILTER set to the base64 of the test's fully qualified name, as described in the repo's runtime-tests docs.
  2. The test fails and prints RESULT: , back, back, front, front (the empty name is the unnamed Grid).

Same thing in app code: take any Panel containing a hit-testable Border, call FindElementsInHostCoordinates(pointInsideBorder, panel), and the Border appears twice, after the panel.

Workaround 🛠️

Use VisualTreeHelper.HitTest / the pointer event's OriginalSource for the top element, or de-duplicate and reverse the result yourself (reversing is only correct when there is no Canvas.ZIndex reordering).

Works on UWP/WinUI

Yes (per docs and the C++ source above; not run natively).

Affected platforms 📱💻🖥️

Verified at runtime on Skia Desktop. The code lives in shared Uno.UI, so it should affect every head.

Uno.SDK version (and other relevant versions) 📦

Current master @ cc48541 (net11.0-desktop build).

IDE version 🧑‍💻

N/A (CLI build).

Anything else we need to know? 💬

Callers that take "the first match" get the back-most/parent-most element instead of the topmost. For example, WinUI.TableView's TableViewRow.ResolvePressedColumn returns the first tagged cell from this enumeration; with frozen (translated, ZIndex=1) cells overlapping scrolled cells, the order matters.

Two sibling bugs in the same function will be filed separately and cross-linked from here: it ignores clipping, and it skips the children of a parent that was not hit. A fix should probably address all three together. Unrelated to #24309, which is about the method returning only ancestors for a Button.

Suggested fix: drop the duplicate yield (either don't yield the child before recursing, or don't yield subtree in the recursive call), and emit results in reverse render order (children in reverse z-order, descendants before their parent), ideally built on the same walk as VisualTreeHelper.HitTest.

Activity

  1. added
    kind/bug
    triage/untriaged
    project/pointers 🖱️
    platform/all
    difficulty/medium 🤔
    on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty/medium 🤔kind/bugplatform/allproject/pointers 🖱️triage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions