Reputation: 3968
Lets say, I've a ListBox
with hundreds of ListBoxItems
. On the right side of the ListBox, there is a ScrollViewer
which allows the user to see the items above or underneath in the current view of the ListBox. My goal is now to determine which elements i.e. ListBoxItems are currently visible. Is there a method to determine if a ListBoxItem is visible on the screen?
Upvotes: 1
Views: 564
Reputation: 1775
You may use ListView instead of ListBox, since ListBoxItem's have a property called Bounds, which allow you to see the client coordinates of each item by returning a Rectangle object. Afterwards, you can compare these coordinates to the ListView's visible client area to determine if an item is visible or not. The visible area in a listview would be from x=0 to x=Width and from y=0 to y=Height. You can quickly test for visibility by using Rectangle.IntersectsWith(Rectangle)...
Upvotes: 0
Reputation: 6953
You can use ListBox.ScrollIntoView() to ensure a ListBoxItem is visible.
Can you use this rather than checking if it is visible?
Upvotes: 1