Share via


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 而且原本就會支援邏輯捲動。對於原本就支援邏輯捲動的版面配置控制項,您仍然可以透過包裝 ScrollViewer 中的主 Panel 項目,並將 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 項目

下列範例在視窗中建立 ScrollViewer,其中會包含某些文字和矩形。ScrollBar 項目只有在必要時才會出現。當您重新調整視窗大小時,因為 ComputedHorizontalScrollBarVisibility 更新值和 ComputedVerticalScrollBarVisibility 屬性的緣故,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 中的文件

請參閱

工作

HOW TO:建立 ScrollViewer

ScrollViewer 樣式範例

概念

Windows Presentation Foundation 中的文件

ScrollBar 的 ControlTemplate 範例

最佳化效能:控制項

參考

ScrollViewer

ScrollBar

IScrollInfo