Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ListView Visible items

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.
like image 677
GameAlchemist Avatar asked Jun 25 '12 10:06

GameAlchemist


1 Answers

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.

like image 145
akjoshi Avatar answered Oct 18 '22 09:10

akjoshi