Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when ListView is scrolled "up" or "down"? Windows Phone 8.1 ListView

Hey how can I detect when my ListView is scrolled up or down?

I have this:

 private void MainPage_OnLoaded(object sender, RoutedEventArgs e)
 {
     var scrollViewer = MyListView.GetFirstDescendantOfType<ScrollViewer>();
     scrollViewer.ViewChanged += BarScroll;
 }

 private void BarScroll(object sender, ScrollViewerViewChangedEventArgs e)
 {
     var scrollbars = (sender as ScrollViewer).GetDescendantsOfType<ScrollBar>().ToList();
     var verticalBar = scrollbars.FirstOrDefault(x => x.Orientation == Orientation.Vertical);

     if (verticalBar) (/*If ListView is scrolled up*/)
     {
        //Code when the ListView is scrolled up
     }
     else (/*If ListView is scrolled down*/)
     {
        //Code for when the ListView is scrolled down
     } 
 }

Do I need to do something with "verticalBar.Height > verticalBar.ActualHeight"?

like image 289
Bayern Avatar asked Dec 16 '15 15:12

Bayern


1 Answers

Modify your BarScroll handler in such way and add private variable to your main class to keep old value of scroll offset:

    private double offset;

    private void BarScroll(object sender, ScrollViewerViewChangedEventArgs e)
    {
        double newOffset = (sender as ScrollViewer).VerticalOffset;
        if ( newOffset > offset )
        {
            //Logic for scroll down
        }
        else if ( newOffset < offset )
        {
            //Logic for scroll up
        }

        offset = newOffset;
    }
like image 50
ASJ Avatar answered Sep 19 '22 13:09

ASJ