共用方式為


如何:使用 IScrollInfo 介面捲動內容

此範例顯示如何使用 IScrollInfo 介面捲動內容。

範例

下列範例示範 IScrollInfo 介面的功能。 此範例會在 Extensible Application Markup Language (XAML) 中建立 StackPanel 元素,其巢狀於父代 ScrollViewer 中。 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 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

另請參閱