如何确定 GridView 中哪些项目在当前滚动窗口中可见? [英] How can I determine which items in a GridView that are visible in the current scroll window?

查看:36
本文介绍了如何确定 GridView 中哪些项目在当前滚动窗口中可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主应用程序页面中有一个 GridView,我想对用户当前查看的项目(高优先级)做一些后台处理,然后当然也做其他项目(低优先级).

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).

我可以访问 ScrollBar 和 ScrollViewer,但它们似乎都没有告诉我我的哪些项目在当前滚动窗口中.我可以尝试破解它,但它变得棘手,因为行/列的数量会根据滚动区域的大小而变化.

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-我们/图书馆/windows/apps/br209745.aspxhttp://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.scrollviewer.aspx

非常感谢任何帮助!

谢谢

推荐答案

您可以通过两种一般方法来做到这一点.首先是获取GridViewItemAutomationPeer,它其实有直接调用的方法.此处的类信息.不过,我在让它实际可用时遇到了问题.幸运的是,还有第二个答案.这需要做一些数学运算,但它是可行的.

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. 使用 GridViewItemContainerGenerator 获取项目容器,在本例中为 GridViewItem.
  2. 获取GridViewScrollViewer.您可以搜索 FindVisualChild 方法,周围有很多.
  3. Do MyGridViewItem.TransformToVisual(MyGridViewScrollViewer).TransformPoint(new Point(0, 0)); 这将使您获得项目的左上角,相对于整个可滚动面板ScrollViewer(称为它的 Extent)(稍后会很重要).
  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).

这将返回一个 Point 对象.

This will return to you a Point object.

接下来,我们需要找出当前显示在 ScrollViewer 中的 X 值的范围.

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

注意:如果您使用的是垂直滚动 ScrollViewer,请使用 Heights.水平,使用 Widths.两个,都用.我将使用水平/宽度来做我的例子.

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. Horizo​​ntalOffset.这是当前可见区域的当前下限".
  2. Horizo​​ntalOffset 加上 ViewportWidth.这是当前可视区域的上限.
  3. 如果您的 Point.X 大于您的下限并且小于您的上限,那么该项目是可见的.
  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.

如果您还需要确定 整个 项目是否可见,请执行Point.X + GridViewItem.Width 的相同计算.

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!

这篇关于如何确定 GridView 中哪些项目在当前滚动窗口中可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆