Search code examples
uwpscrollviewerdirection

Detect direction of scrolling in UWP apps


I'm developing a UWP app in C# and am using a ScrollViewer to scroll some content. When the user scrolls vertically I want to know if there is a way to detect whether the user is scrolling top-to-bottom or bottom-to-top (i.e direction of scrolling) ?


Solution

  • You can take the help from the VerticalOffset property of ScrollViewer to detect the movement if the value is increasing the user is scrolling form top to down. If the value is decreasing then user is scrolling from bottom to top. You can listen the ViewChanged event of ScrollViewer and do the necessary logic. Below is sample Binding to demonstrate the idea.

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock VerticalAlignment="Center" Text="{Binding VerticalOffset, ElementName=tst,Mode=OneWay}"/>
    
        <ScrollViewer Grid.Row="1" x:Name="tst" ViewChanged="tst_ViewChanged">
            <StackPanel Background="Gray" Height="2500">
            </StackPanel>
        </ScrollViewer>
    </Grid>
    

    Output:enter image description here