System.Data
System.Data

Reputation: 3968

Determine the visibility of a control on screen

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

Answers (2)

Eugenio De Hoyos
Eugenio De Hoyos

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

Steve Chadbourne
Steve Chadbourne

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

Related Questions