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"?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With