Determine the visibility of a control on screen
Lets say, I've a ListBox
with hundreds of ListBoxItems
. On the ri开发者_StackOverflow中文版ght 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?
You can use ListBox.ScrollIntoView() to ensure a ListBoxItem is visible.
Can you use this rather than checking if it is visible?
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)...
精彩评论