How to detect ListView is scrolling up or down
Asked Answered
V

2

6

Is there a way to detect that ScrollViwer of ListView is in scrolling mode and stopped scrolling. In windows phone 8.1 ListView we can not get reference of the scrollviewer.

Any one done it in windows phone 8.1 WinRT app?

Voguish answered 4/12, 2014 at 12:44 Comment(3)
As I think, you should accept @yasen answer - he was faster (3 seconds) and after edit has much better method of traversing the VisualTree ;)Pyrrhotite
Thanks for mentioning. I have changed the accepted answer thanks again :)Voguish
@Pyrrhotite Thanks. Your answer is good too, as always. :) Only a bit less generic.Evered
E
6

Once the ListView is Loaded you can get the ScrollViewer like this:

var sv = (ScrollViewer)VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(this.ListV, 0), 0);

Edit

As Romasz suggested, once you get the ScrollViewer, you can use its ViewChanged event, to monitor when it is scrolling and when it stops.

Also, here's the generic extension method that I use for traversing the visual tree:

// The method traverses the visual tree lazily, layer by layer
// and returns the objects of the desired type
public static IEnumerable<T> GetChildrenOfType<T>(this DependencyObject start) where T : class 
{
    var queue = new Queue<DependencyObject>();
    queue.Enqueue(start);

    while (queue.Count > 0) {
        var item = queue.Dequeue();

        var realItem = item as T;
        if (realItem != null) {
             yield return realItem;
        }

        int count = VisualTreeHelper.GetChildrenCount(item);
        for (int i = 0; i < count; i++) {
            queue.Enqueue(VisualTreeHelper.GetChild(item, i));
        }
    }
}

To get the ScrollViewer using this methos, do this:

var sv = yourListView.GetChildrenOfType<ScrollViewer>().First();
Evered answered 4/12, 2014 at 12:55 Comment(0)
P
4

You can find the ScrollViewer of your ListView by using VisualTreeHelper. For example like this:

// method to pull out a ScrollViewer
public static ScrollViewer GetScrollViewer(DependencyObject depObj)
{
    if (depObj is ScrollViewer) return depObj as ScrollViewer;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        var child = VisualTreeHelper.GetChild(depObj, i);

        var result = GetScrollViewer(child);
        if (result != null) return result;
    }
    return null;
}

Once you have a ScrollViewer you can subscribe to its events:

GetScrollViewer(yourListView).ViewChanged += yourEvent_ViewChanged;
Pyrrhotite answered 4/12, 2014 at 12:55 Comment(6)
I am using IsupportIncrementalLoading extended class for populating the list view, where the items are loaded from internet. How can I get the scrollviewer in this case, since the listview take some time to populate the child items.Copious
@Copious And where is the problem - what are you trying to achieve? As I think ListView has one scrollviewer and incremental loading just loads items when needed.Pyrrhotite
The problem is how can I get a scrollviewer? I called the getscrollviewer method inside the listview loaded method. But at that time the listview has no child items. i.e GetChildrenCount returns 0.Copious
@Copious I've not played with that much. In this case I think it will be more suitable just to ask new question on SO.Pyrrhotite
Worked. Thanks. I should research more before I ask a question. Sorry!Copious
@Copious I'm not sure why you have deleted the question - it was a good one - may help somebody in future.Pyrrhotite

© 2022 - 2024 — McMap. All rights reserved.