GameAlchemist GameAlchemist - 1 year ago 218 Question

Get ListView Visible items

I have a

which might contains a lot of items, so it is
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
or in the
, 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
Recycling ListView
with its
bound to an

Edit :
Answer :

thanks to akjoshi, I found the way :

  • get the
    of the

    (with a
    method, that you can do yourself with the

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

  • read its
    : it is the count of items shown.

    Rq :
    must be true.

Answer Source

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.


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download