Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine which items in a GridView that are visible in the current scroll window?

I have a GridView in my main app page and I want to do some background processing for the items that are currently in view for the user (high priority), and then of course do the other items too (low priority).

I can access the ScrollBar and the ScrollViewer, but none of them appear to tell me which of my items are in the current scroll window. I could try to hack this in, but it gets tricky because the number of row/columns change based on the size of the scroll region.

http://msdn.microsoft.com/en-us/library/windows/apps/br209745.aspx http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.scrollviewer.aspx

Any help much appreciated!

thanks

like image 632
swinefeaster Avatar asked Oct 28 '25 14:10

swinefeaster


1 Answers

There are two general ways you can do this. The first is to get the GridViewItem's AutomationPeer, which actually has a direct method to call. Class information here. I've had problems getting this to actually be usable though. Luckily, there's a second answer. It will require some math to be done, but it's doable.

  1. Get the item container, in this case a GridViewItem, using the GridView's ItemContainerGenerator.
  2. Get the GridView's ScrollViewer. You can search for FindVisualChild<T> methods, there are plenty around.
  3. Do MyGridViewItem.TransformToVisual(MyGridViewScrollViewer).TransformPoint(new Point(0, 0)); This will get you the top left corner of the item, relative to the entire scrollable panel (known as its Extent) of the ScrollViewer (this will be important later).

This will return to you a Point object.

Next, we'll need to find out the range of X values that are currently being displayed in the ScrollViewer.

Note: If you're using a Vertical scrolling ScrollViewer, use the Heights. Horizontal, use the Widths. Both, use both. I am going to do my example using the Horizontal/Width.

  1. Take the HorizontalOffset. This is your current 'lower bound' for the current viewable region.
  2. Take the HorizontalOffset plus the ViewportWidth. This is the upper bound of the current viewable region.
  3. If your Point.X is greater than your lower bound and less than your upper bound, then the item is visible.

If you further need to find out if the whole item is visible, do this same calculation for Point.X + GridViewItem.Width.

Hope this helps, and happy coding!

like image 120
Nate Diamond Avatar answered Oct 30 '25 08:10

Nate Diamond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!