ScrollViewer 概述

更新:2007 年 11 月

用户界面中的内容通常比计算机屏幕的显示区域大。利用 ScrollViewer 控件可以方便地使 Windows Presentation Foundation (WPF) 应用程序中的内容具备滚动功能。本主题介绍 ScrollViewer 元素,并提供若干用法示例。

本主题包含以下各节:

  • ScrollViewer 控件

  • 物理滚动与逻辑滚动

  • 定义和使用 ScrollViewer 元素

  • 设置 ScrollViewer 的样式

  • 对文档进行分页

  • 相关主题

ScrollViewer 控件

WPF 应用程序中有两个具备滚动功能的预定义元素:ScrollBarScrollViewerScrollViewer 控件封装了水平和垂直 ScrollBar 元素以及一个内容容器(如 Panel 元素),以便在可滚动的区域中显示其他可见元素。您必须建立自定义对象才能使用 ScrollBar 元素实现内容滚动。不过,您可以单独使用 ScrollViewer 元素,因为它是一个封装了 ScrollBar 功能的复合控件。

ScrollViewer 控件既响应鼠标命令,也响应键盘命令,并定义了许多可用于按预设的增量滚动内容的方法。可以使用 ScrollChanged 事件来检测 ScrollViewer 状态的更改。

ScrollViewer 只能有一个子项,通常是 Panel 元素,该元素可承载 UIElementsChildren 集合。Content 属性定义 ScrollViewer 的唯一子项。

物理滚动与逻辑滚动

物理滚动用于按预设的物理增量(通常按以像素为单位声明的值)滚动内容。逻辑滚动用于滚动到逻辑树中的下一项。物理滚动是大多数 Panel 元素的默认滚动行为。WPF 同时支持两种类型的滚动。

IScrollInfo 接口

IScrollInfo 接口表示 ScrollViewer 或派生控件内的主滚动区域。该接口定义可由 Panel 元素实现的滚动属性和方法,这些元素需要按逻辑单位(而不是按物理增量)滚动。通过将 IScrollInfo 的实例转换为派生的 Panel,然后使用其滚动方法,从而提供了一种有用的方法,可以滚动到子集合中的下一个逻辑单位,而不是按像素增量滚动。默认情况下,ScrollViewer 控件支持按物理单位滚动。

StackPanelVirtualizingStackPanel 都实现了 IScrollInfo,并以本机方式支持逻辑滚动。对于以本机方式支持逻辑滚动的布局控件,您仍然可以通过将宿主 Panel 元素放在 ScrollViewer 中并将 CanContentScroll 属性设置为 false 来实现物理滚动。

下面的代码示例演示如何将 IScrollInfo 的实例转换为 StackPanel,并使用接口定义的内容滚动方法(LineUpLineDown)。

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

下面的示例在窗口中创建包含一些文本和一个矩形的 ScrollViewerScrollBar 元素只有在必要时才会出现。当您调整窗口大小时,由于 ComputedHorizontalScrollBarVisibilityComputedVerticalScrollBarVisibility 属性更新了值,ScrollBar 元素将出现然后消失。

说明:

有关完整的代码示例,请参见 ScrollViewer 示例

'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 Border
myScrollViewer.Content = myStackPanel
Me.Content = myScrollViewer
// 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 Border
myScrollViewer.Content = myStackPanel;

// Add the Border as the Content of the Parent Window Object
mainWindow.Content = myScrollViewer;
mainWindow.Show ();

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="https://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 的样式以便更改该控件的默认呈现行为。有关自定义样式的 ScrollViewer 的示例,请参见 ScrollViewer 样式示例。有关控件样式设置的附加信息,请参见样式设置和模板化

对文档进行分页

对于文档内容,一种替代滚动的方法是选择支持分页的文档容器。在设计上,FlowDocuments 文档可以寄宿在查看控件内,比如 FlowDocumentPageViewer,该控件支持将跨多个页面的内容分页,从而无需进行滚动。DocumentViewer 提供了用于查看 FixedDocument 内容的解决方法,该解决方法使用传统的滚动来显示超出显示区域范围的内容。

有关文档格式和表示形式选项的附加信息,请参见 Windows Presentation Foundation 中的文档

请参见

任务

如何:创建 ScrollViewer

ScrollViewer 样式示例

概念

Windows Presentation Foundation 中的文档

ScrollBar ControlTemplate 示例

优化性能:控件

参考

ScrollViewer

ScrollBar

IScrollInfo