Please login to access all of the features of the Community Site.

ScrollToVisible

M v1.0

There are two ways to initiate this command, we'll show them below. But the basic syntax is you specify the Anchor element that should be scrolled until it becomes visible. Think of this like the HTML Anchor href, or a "jump to" button.

You can either pass the view to be made visible directly by reference in the CommandParameter or you can pass an instance of the ScrollToVisibleParameters object.

Properties

PropertyTypeDescription
AnchorVisualElementThe view which resides inside a ScrollView that should be made visible.
PositionScrollToPosition  The position to put the view at after scrolling (see below). Defaults to MakeVisible
AnimatedBoolean  Sets whether the scroll should be animated. Defaults to True

The options you have with the Position parameter are as follows:

  • MakeVisible - Just make sure the anchor is visible on screen
  • Start - Attempt to scroll until the anchor is at the start (top or left) of the screen
  • Center - Attempt to scroll until the anchor is at the center of the screen
  • End - Attempt to scroll until the anchor is at the end (bottom or right) of the screen

Examples

<VerticalStackLayout>
    <Button Text="Scroll"
        Command="{Binding ScrollToVisible}"
        CommandParameter="{x:Reference myLabel}" />

    <BoxView Color="Red"
        HeightRequest="1200" />

    <Label Text="My Label"
        x:Name="myLabel" />

    <BoxView Color="Blue"
        HeightRequest="1200" />
</VerticalStackLayout>

The above will scroll the first ScrollView in the view tree above the Label we specified as our anchor so that the label is visible. This by itself may be just fine for what you need, but it may not end up doing what you want. By default, the anchor will be scrolled until it becomes "just visible". In this case, since we need to scroll down, the label will end up at the bottom of the screen.

To accommodate those situations, you can specify a parameters object like so.

<Button Text="Scroll"
    Command="{Binding ScrollToVisible}">
    <Button.CommandParameter>
        <Rock:ScrollToVisibleParameters Anchor="{x:Reference myLabel}"
            Position="Start" />
    </Button.CommandParameter>
</Button>

This still indicates the same Label we want to use as the Anchor, but it also specifies the position we want it to end up at. Now, this is not a forced position. If there were nothing below the label, it wouldn't be possible for it to end up at the top. But since we have a lot of content below as well, there is enough room to scroll.

Finally, due to the way XAML works, there is a shorthand to the XAML above.

<Button Text="Scroll"
    Command="{Binding ScrollToVisible}"
    CommandParameter="{Rock:ScrollToVisibleParameters Anchor={x:Reference myLabel}, Position=Start}" />