此示例演示如何使用 IScrollInfo 接口滚动内容。
示例:
以下示例演示 IScrollInfo 接口的功能。 该示例在可扩展应用程序标记语言(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 中的滚动行为。 下面的示例演示如何使用 LineUp 和 LineDown 方法;它还一般演示如何使用类定义的所有定位方法 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