Condividi tramite


How to: Scroll Content by Using the IScrollInfo Interface

This example shows how to scroll content by using the IScrollInfo interface.

Example

The following example demonstrates the features of the IScrollInfo interface. The example creates a StackPanel element in Extensible Application Markup Language (XAML) that is nested in a parent ScrollViewer. The child elements of the StackPanel can be scrolled logically by using the methods defined by the IScrollInfo interface and cast to the instance of StackPanel (sp1) in code.

<Border BorderBrush="Black" Background="White" BorderThickness="2" Width="500" Height="500">
    <ScrollViewer Name="sv1" CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
        <StackPanel Name="sp1">
            <Button>Button 1</Button>
            <Button>Button 2</Button>
            <Button>Button 3</Button>
            <Button>Button 4</Button>
            <Button>Button 5</Button>
            <Rectangle Width="700" Height="500" Fill="Purple"/>
            <TextBlock>Rectangle 1</TextBlock>
            <Rectangle Width="700" Height="500" Fill="Red"/>
            <TextBlock>Rectangle 2</TextBlock>
            <Rectangle Width="700" Height="500" Fill="Green"/>
            <TextBlock>Rectangle 3</TextBlock>
        </StackPanel> 
    </ScrollViewer>
</Border>

Each Button in the XAML file triggers an associated custom method that controls scrolling behavior in StackPanel. The following example shows how to use the LineUp and LineDown methods; it also generically shows how to use all the positioning methods that the IScrollInfo class defines.

private void spLineUp(object sender, RoutedEventArgs e)
{
    ((IScrollInfo)sp1).LineUp();
}
private void spLineDown(object sender, RoutedEventArgs e)
{
    ((IScrollInfo)sp1).LineDown();
}
Private Sub spLineUp(ByVal sender As Object, ByVal args As RoutedEventArgs)

    CType(sp1, IScrollInfo).LineUp()
End Sub
Private Sub spLineDown(ByVal sender As Object, ByVal args As RoutedEventArgs)

    CType(sp1, IScrollInfo).LineDown()
End Sub

For the complete sample, see IScrollInfo Methods Sample.

See Also

Reference

ScrollViewer
IScrollInfo
StackPanel

Concepts

ScrollViewer Overview
Panels Overview

Other Resources

IScrollInfo Methods Sample
ScrollViewer Samples
ScrollViewer How-to Topics