Nir Hasson
Topic Author
Posts: 71
Joined: 10 Nov 2013, 21:20
Contact:

How to detect if a list box item is visible?

22 Jan 2021, 08:31

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)
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

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

22 Jan 2021, 13:10

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?
 
Nir Hasson
Topic Author
Posts: 71
Joined: 10 Nov 2013, 21:20
Contact:

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

24 Jan 2021, 07:53

That seems to work perfectly, thanks!
I've used the scroll viewer offset approach before but it didn't feel like the right solution.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

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

25 Jan 2021, 10:53

Great, marking it as solved.

Who is online

Users browsing this forum: Google [Bot] and 90 guests