用户界面中的内容通常大于计算机屏幕的显示区域。 该 ScrollViewer 控件提供了一种方便的方法,用于在 Windows Presentation Foundation (WPF) 应用程序中滚动内容。 本主题介绍该 ScrollViewer 元素,并提供多个用法示例。
ScrollViewer 控件
在 WPF 应用程序中启用滚动的两个预定义元素: ScrollBar 和 ScrollViewer。 该 ScrollViewer 控件封装水平元素和垂直 ScrollBar 元素以及内容容器(如 Panel 元素),以便在可滚动区域中显示其他可见元素。 必须生成自定义对象才能使用 ScrollBar 元素进行内容滚动。 但是,可以单独使用该 ScrollViewer 元素,因为它是封装 ScrollBar 功能的复合控件。
该 ScrollViewer 控件同时响应鼠标和键盘命令,并定义许多方法,通过预先确定的增量滚动内容。 可以使用该 ScrollChanged 事件来检测状态中的 ScrollViewer 更改。
一个 ScrollViewer 子级只能有一个 Panel 子级,通常是可以承载 Children 元素集合的元素。 该 Content 属性定义唯一的 ScrollViewer子级 。
物理滚动与逻辑滚动
物理滚动用于按预先确定的物理增量滚动内容,通常由以像素为单位声明的值。 逻辑滚动用于滚动到逻辑树中的下一项。 物理滚动是大多数 Panel 元素的默认滚动行为。 WPF 支持这两种类型的滚动。
IScrollInfo 接口
该 IScrollInfo 接口表示控件或派生控件中的 ScrollViewer 主滚动区域。 该接口定义滚动属性和方法,这些属性和方法可由需要按 Panel 逻辑单元滚动的元素实现,而不是按物理增量实现。 将派生实例 IScrollInfo 强制转换为派生 Panel 的实例,然后使用其滚动方法提供了一种有用的方法来滚动到子集合中的下一个逻辑单元,而不是按像素增量滚动。 默认情况下,控件 ScrollViewer 支持按物理单位滚动。
StackPanel 和 VirtualizingStackPanel 都实现了 IScrollInfo 并且本机支持逻辑滚动。 对于本机支持逻辑滚动的布局控件,你仍可以通过将主机 Panel 元素包装在 ScrollViewer 中,并将 CanContentScroll 属性设置为 false
来实现物理滚动。
下面的代码示例演示如何将一个IScrollInfo实例强制转换为StackPanel,并使用接口定义的内容滚动方法(LineUp和LineDown)。
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
定义和使用 ScrollViewer 元素
以下示例在包含一些文本和矩形的窗口中创建 ScrollViewer 一个。 ScrollBar 元素仅在必要时才显示。 调整窗口大小时,由于ScrollBar和ComputedHorizontalScrollBarVisibility属性的值已更新,ComputedVerticalScrollBarVisibility元素将显示或隐藏。
// Create the application's main window
mainWindow = gcnew System::Windows::Window();
mainWindow->Title = "ScrollViewer Sample";
// Define a ScrollViewer
myScrollViewer = gcnew ScrollViewer();
myScrollViewer->HorizontalScrollBarVisibility = ScrollBarVisibility::Auto;
// Add Layout control
myStackPanel = gcnew StackPanel();
myStackPanel->HorizontalAlignment = HorizontalAlignment::Left;
myStackPanel->VerticalAlignment = VerticalAlignment::Top;
TextBlock^ myTextBlock = gcnew TextBlock();
myTextBlock->TextWrapping = TextWrapping::Wrap;
myTextBlock->Margin = System::Windows::Thickness(0, 0, 0, 20);
myTextBlock->Text = "Scrolling is enabled when it is necessary. Resize the Window, making it larger and smaller.";
Rectangle^ myRectangle = gcnew Rectangle();
myRectangle->Fill = Brushes::Red;
myRectangle->Width = 500;
myRectangle->Height = 500;
// Add child elements to the parent StackPanel
myStackPanel->Children->Add(myTextBlock);
myStackPanel->Children->Add(myRectangle);
// Add the StackPanel as the lone child of the ScrollViewer
myScrollViewer->Content = myStackPanel;
// Add the ScrollViewer as the Content of the parent Window object
mainWindow->Content = myScrollViewer;
mainWindow->Show();
// Create the application's main window
mainWindow = new Window ();
mainWindow.Title = "ScrollViewer Sample";
// Define a ScrollViewer
myScrollViewer = new ScrollViewer();
myScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
// Add Layout control
myStackPanel = new StackPanel();
myStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
myStackPanel.VerticalAlignment = VerticalAlignment.Top;
TextBlock myTextBlock = new TextBlock();
myTextBlock.TextWrapping = TextWrapping.Wrap;
myTextBlock.Margin = new Thickness(0, 0, 0, 20);
myTextBlock.Text = "Scrolling is enabled when it is necessary. Resize the Window, making it larger and smaller.";
Rectangle myRectangle = new Rectangle();
myRectangle.Fill = Brushes.Red;
myRectangle.Width = 500;
myRectangle.Height = 500;
// Add child elements to the parent StackPanel
myStackPanel.Children.Add(myTextBlock);
myStackPanel.Children.Add(myRectangle);
// Add the StackPanel as the lone child of the ScrollViewer
myScrollViewer.Content = myStackPanel;
// Add the ScrollViewer as the Content of the parent Window object
mainWindow.Content = myScrollViewer;
mainWindow.Show ();
'Define a ScrollViewer.
Dim myScrollViewer As New ScrollViewer
myScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
'Add Layout control.
Dim myStackPanel As New StackPanel
myStackPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Left
myStackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Top
Dim myTextBlock As New TextBlock
myTextBlock.TextWrapping = TextWrapping.Wrap
myTextBlock.Margin = New Thickness(0, 0, 0, 20)
myTextBlock.Text = "Scrolling is enabled when it is necessary. Resize the Window, making it larger and smaller."
Dim myRectangle As New Rectangle
myRectangle.Fill = Brushes.Red
myRectangle.Width = 500
myRectangle.Height = 500
'Add child elements to the parent StackPanel.
myStackPanel.Children.Add(myTextBlock)
myStackPanel.Children.Add(myRectangle)
'Add the StackPanel as the lone child of the ScrollViewer
myScrollViewer.Content = myStackPanel
'Add the ScrollViewer as the Content of the parent Window object
Me.Content = myScrollViewer
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="ScrollViewer Sample">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBlock TextWrapping="Wrap" Margin="0,0,0,20">Scrolling is enabled when it is necessary.
Resize the window, making it larger and smaller.</TextBlock>
<Rectangle Fill="Red" Width="500" Height="500"></Rectangle>
</StackPanel>
</ScrollViewer>
</Page>
设置 ScrollViewer 样式
与 Windows Presentation Foundation 中的所有控件一样, ScrollViewer 可以设置样式以更改控件的默认呈现行为。 有关控件样式的其他信息,请参阅 “样式设置”和“模板化”。
分页文档
对于文档内容,滚动的替代方法是选择支持分页的文档容器。 FlowDocument 适用于设计为托管在查看控件内的文档,例如 FlowDocumentPageViewer,该控件支持将内容分页到多个页面,从而避免滚动的需要。 DocumentViewer 提供了一种用于查看 FixedDocument 内容的解决方案,该解决方案使用传统滚动在显示区域之外显示内容。
有关文档格式和演示文稿选项的其他信息,请参阅 WPF 中的文档。