How to detect ListView is scrolling up or down

后端 未结 3 875
耶瑟儿~
耶瑟儿~ 2020-12-10 07:18

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

3条回答
  •  伪装坚强ぢ
    2020-12-10 07:53

    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 GetChildrenOfType(this DependencyObject start) where T : class 
    {
        var queue = new Queue();
        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().First();
    

提交回复
热议问题