检测ListView何时滚动" up"或" down"? Windows Phone 8.1 ListView

时间:2015-12-16 15:21:54

标签: c# listview windows-phone-8.1 scrollbar

嘿我怎样才能检测到我的ListView向上或向下滚动?

我有这个:

 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
     } 
 }

我是否需要对&#34; verticalBar.Height&gt;执行某些操作? verticalBar.ActualHeight&#34;

1 个答案:

答案 0 :(得分:6)

以这种方式修改BarScroll处理程序并将私有变量添加到主类中以保留旧的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;
    }