Page 1 of 1

How to detect if a list box item is visible?

Posted: 22 Jan 2021, 08:31
by Nir Hasson
Using Noesis 3.0.9 on Unity.
How can I know if specific list box item is currently visible to the user? (I.E being actually rendered on the screen)

Re: How to detect if a list box item is visible?

Posted: 22 Jan 2021, 13:10
by sfernandez
Assuming you can get access to both the ListBox and ListBoxItem, you just need to compare their content bounds:
private bool IsElementVisible(FrameworkElement element, FrameworkElement container)
{
    if (element == null || container == null || !element.IsVisible || !element.IsDescendantOf(container))
        return false;

    Rect elementBounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
    Rect containerBounds = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
    return containerBounds.IntersectsWith(elementBounds);
}
Does that work for you?

Re: How to detect if a list box item is visible?

Posted: 24 Jan 2021, 07:53
by Nir Hasson
That seems to work perfectly, thanks!
I've used the scroll viewer offset approach before but it didn't feel like the right solution.

Re: How to detect if a list box item is visible?

Posted: 25 Jan 2021, 10:53
by sfernandez
Great, marking it as solved.