共用方式為


ScrollViewer 概觀

使用者介面內的內容常常會大於電腦螢幕的顯示區域。 ScrollViewer 控制項提供一個簡便的方式,可讓您捲動 Windows Presentation Foundation (WPF) 應用程式中的內容。 本主題介紹 ScrollViewer 元素,並提供數個使用範例。

ScrollViewer 控制項

WPF 應用程式中有兩個啟用捲動的預先定義元素:ScrollBarScrollViewerScrollViewer 控制項會封裝水平和垂直 ScrollBar 元素和內容容器 (例如 Panel 元素),以便在可捲動區域中顯示其他可見元素。 您必須組建自定義物件,才能使用 ScrollBar 元素進行內容捲動。 不過,您可以單獨使用 ScrollViewer 元素,因為它是封裝 ScrollBar 功能的複合控件。

ScrollViewer 控制項可以回應滑鼠和鍵盤命令,並定義了許多方法,可依預先決定的遞增值來捲動內容。 您可以使用 ScrollChanged 事件來偵測 ScrollViewer 狀態的變更。

ScrollViewer 只能有一個子項目,通常是可以裝載 Panel 元素集合的 Children 元素。 Content 屬性會定義 ScrollViewer 的唯一子項目。

實體捲動與邏輯捲動的比較

實體捲動可用來依預先決定的實體遞增值 (通常是以像素為單位宣告的值) 捲動內容。 邏輯捲動可用來捲動至邏輯樹狀結構中的下一個項目。 實體捲動是大多數 Panel 元素的預設捲動行為。 WPF 同時支援這兩種類型的捲動。

IScrollInfo 介面

IScrollInfo 介面代表 ScrollViewer 或衍生控制項內的主要捲動區域。 此介面定義可由需要依邏輯單元 (而非依實體遞增值) 進行捲動的 Panel 元素實作的捲動屬性和方法。 您可以先將 IScrollInfo 執行個體轉換成衍生的 Panel,再使用其捲動方法,這個做法提供一個實用的方式來捲動至子集合中的下一個邏輯單元,而不須依像素遞增值進行捲動。 根據預設,ScrollViewer 控制項支援依實體單位捲動。

StackPanelVirtualizingStackPanel 都會實作 IScrollInfo,並原生支持邏輯捲動。 對於原生支援邏輯捲動的版面配置控制項,您仍然可以將託管 Panel 元素包裝在 ScrollViewer 中,並將 CanContentScroll 屬性設定為 false,以達到實體捲動。

下列程式碼範例示範如何將 IScrollInfo 執行個體轉換成 StackPanel,並使用介面所定義的內容捲動方法 (LineUpLineDown)。

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

下列範例會在一個包含一些文字和一個矩形的視窗中建立 ScrollViewerScrollBar 元素只有在必要時才會出現。 當您調整視窗大小時,ScrollBar 元素會因為 ComputedHorizontalScrollBarVisibilityComputedVerticalScrollBarVisibility 屬性的更新值而出現和消失。


// 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 中的文件

另請參閱