如何:使用 IScrollInfo 接口来滚动内容

此示例演示如何使用 IScrollInfo 接口来滚动内容。

示例

下面的示例演示 IScrollInfo 接口的功能。 该示例使用 Extensible Application Markup Language (XAML) 创建了一个嵌套在父 ScrollViewer 中的 StackPanel 元素。 StackPanel 的子元素逻辑上可以使用由 IScrollInfo 接口定义的方法进行滚动,并且可以在代码中强制转换为 StackPanel (sp1) 的实例。

<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>

XAML 文件中的每个 Button 将触发一个相关的自定义方法,该方法控制 StackPanel 中的滚动行为。 下面的示例演示如何使用 LineUpLineDown 方法;它还大概地演示了如何使用 IScrollInfo 类定义的所有定位方法。

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
private void spLineUp(object sender, RoutedEventArgs e)
{
    ((IScrollInfo)sp1).LineUp();
}
private void spLineDown(object sender, RoutedEventArgs e)
{
    ((IScrollInfo)sp1).LineDown();
}

请参见

参考

ScrollViewer

IScrollInfo

StackPanel

概念

ScrollViewer 概述

面板概述

其他资源

ScrollViewer 帮助主题