获取 ListView 可见项 [英] Get ListView Visible items

查看:43
本文介绍了获取 ListView 可见项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView 可能包含很多项目,所以它是 virtualized 和回收项目.它不使用排序.我需要刷新一些值显示,但是当项目太多时,更新所有内容太慢,所以我只想刷新可见项目.

我如何获得当前显示的所有项目的列表?我试图查看 ListViewScrollViewer,但我仍然不知道如何实现这一点.解决方案不能通过所有项目来测试它们是否可以被看到,因为这太慢了.

我不确定代码或 xaml 是否有用,它只是一个 Virtualized/Recycling ListView,其 ItemSource 绑定到一个数组.

答案:
感谢 akjoshi,我找到了方法:

  • 获取ListViewScrollViewer(使用 FindDescendant 方法,您可以使用 VisualTreeHelper 自己完成).

  • 读取它的 ScrollViewer.VerticalOffset :它是显示的第一项的编号

  • 读取它的 ScrollViewer.ViewportHeight :它是显示的项目数.
    Rq : CanContentScroll 必须为真.

解决方案

看看 MSDN 上的这个问题,展示了一种找出可见 ListView 项目的技术 -

如何查找行(ListViewItem(s)) 在实际可见的 ListView 中?

这是该帖子的相关代码 -

listView.ItemsSource = from i in Enumerable.Range(0, 100) select "Item" + i.ToString();listView.Loaded += (sender, e) =>{ScrollViewer scrollViewer = listView.GetVisualChild();//扩展方法如果(滚动查看器!= null){ScrollBar scrollBar = scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer) as ScrollBar;如果(滚动条!= null){scrollBar.ValueChanged += 委托{//VerticalOffset 和 ViweportHeight 如果开启了 UI 虚拟化,其实就是你想要的.Console.WriteLine("可见项起始索引:{0}", scrollViewer.VerticalOffset);Console.WriteLine("可见项目数:{0}", scrollViewer.ViewportHeight);};}}};

您应该做的另一件事是使用 ObservableCollection 作为您的 ItemSource 而不是 Array;这肯定会提高性能.

更新:

是的,这可能是真的(array vs. ObservableCollection),但我想看到一些与此相关的统计数据;

ObservableCollection 的真正好处是,如果您需要在运行时从 ListView 中添加/删除项目,以防 Array 你将不得不重新分配 ListViewItemSource 并且 ListView 首先扔掉它以前的项目并重新生成它的整个列表.>

I have a ListView which might contains a lot of items, so it is virtualized and recycling items. It does not use sort. I need to refresh some value display, but when there are too many items, it is too slow to update everything, so I would like to refresh only the visible items.

How could I get a list of all currently displayed items ? I tried to look into the ListView or in the ScrollViewer, but I still have no idea how to achieve this. The solution must NOT go through all items to test if they can be seen, because this would be too slow.

I'm not sure code or xaml would be useful, it is just a Virtualized/Recycling ListView with its ItemSource bound to an Array.

Edit : Answer :
thanks to akjoshi, I found the way :

  • get the ScrollViewer of the ListView (with a FindDescendant method, that you can do yourself with the VisualTreeHelper ).

  • read its ScrollViewer.VerticalOffset : it is the number of the first item shown

  • read its ScrollViewer.ViewportHeight : it is the count of items shown.
    Rq : CanContentScroll must be true.

解决方案

Have a look at this question on MSDN showing a technique to find out the visible ListView items -

How to find the rows (ListViewItem(s)) in a ListView that are actually visible?

Here's the relevant code from that post -

listView.ItemsSource = from i in Enumerable.Range(0, 100) select "Item" + i.ToString();
listView.Loaded += (sender, e) =>
{
    ScrollViewer scrollViewer = listView.GetVisualChild<ScrollViewer>(); //Extension method
    if (scrollViewer != null)
    {
        ScrollBar scrollBar = scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer) as ScrollBar;
        if (scrollBar != null)
        {
            scrollBar.ValueChanged += delegate
            {
                //VerticalOffset and ViweportHeight is actually what you want if UI virtualization is turned on.
                Console.WriteLine("Visible Item Start Index:{0}", scrollViewer.VerticalOffset);
                Console.WriteLine("Visible Item Count:{0}", scrollViewer.ViewportHeight);
            };
        }
    }
};

Another thing you should do is to use ObservableCollection as your ItemSource instead of an Array; that will definitely improve the performance.

Update:

Ya that might be true(array vs. ObservableCollection) but I would like to see some statistics related to this;

The real benefit of ObservableCollection is if you have a requirement to add/remove items from your ListView at run-time, in case of an Array you will have to reassign the ItemSource of ListView and the ListView first throws away its previous items and regenerates its entire list.

这篇关于获取 ListView 可见项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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