Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

c# - UWP XAML ScrollViewer how to show visual indicator of scrollable content

Is there a way to visually indicate that there is scrollable content below the visible area of a <ScrollViewer>?

I see this effect in applications like Microsoft Teams. A shadow appears at the bottom of the scrollable area to indicate that more content is present.

None of the properties of <ScrollViewer> seem to be a clear match. But I'm hoping I can avoid having to programmatically show/hide an element below the <ScrollViewer> based on the scroll position.

enter image description here

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have to say that currently there is no built-in API in ScrollViewer that could directly show a shadow there if the content is not ended.

You might still need to check it programmatically by handling the ViewChanged event of the ScrollViewer and add a custom element.

You could use the following code to check if the ScrollViewer reaches the end:

    <Grid>
    <ScrollViewer Name="MyScrollViewer" ViewChanged="MyScrollViewer_ViewChanged">
        <Rectangle
            x:Name="MyRectangle"
            Width="3000"
            Height="3000"
            Fill="Blue"
            Margin="20" />
    </ScrollViewer>
</Grid>

Code behind:

  private void MyScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
    {
        var verticalOffset = MyScrollViewer.VerticalOffset;
        var maxVerticalOffset = MyScrollViewer.ScrollableHeight;

        if (maxVerticalOffset < 0 || verticalOffset == maxVerticalOffset)
        {
            // content reaches the end
            MyRectangle.Fill = new SolidColorBrush(Colors.Red);
        }
        else
        {
            // content does not reach the end
            MyRectangle.Fill = new SolidColorBrush(Colors.Blue);
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...