次の方法で共有


ScrollViewer の概要

ユーザー インターフェイス内のコンテンツは、多くの場合、コンピューター画面の表示領域よりも大きいです。 ScrollViewer コントロールでは、Windows Presentation Foundation (WPF) アプリケーション内のコンテンツのスクロールを有効にする便利な手段が提供されます。 このトピックでは、ScrollViewer 要素について説明し、いくつかの使用例を示します。

ScrollViewer コントロール

WPF アプリケーションでのスクロールを有効にする 2 つの定義済みの要素 ScrollBarScrollViewer があります。 ScrollViewer コントロールには、水平方向および垂直方向の ScrollBar 要素と、スクロール可能領域内に他の表示可能要素を表示するためのコンテンツ コンテナー (Panel 要素など) がカプセル化されています。 コンテンツのスクロールに ScrollBar 要素を使用するには、カスタム オブジェクトを作成する必要があります。 ただし、ScrollViewer 要素は、ScrollBar の機能をカプセル化する複合コントロールであるため、単独で使用できます。

ScrollViewer コントロールはマウスとキーボードの両方のコマンドに応答し、このコントロールでは事前に決められた増分でコンテンツをスクロールするためのさまざまなメソッドが定義されています。 ScrollChanged イベントを使用して、ScrollViewer の状態の変化を検出できます。

ScrollViewer で保持できる子は 1 つだけであり、通常は、要素の Children コレクションをホストできる Panel 要素です。 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 要素の定義と使用

次の例では、いくつかのテキストと四角形を含む ScrollViewer をウィンドウ内に作成します。 ScrollBar 要素は、必要なときにだけ表示されます。 ウィンドウのサイズを変更すると、ComputedHorizontalScrollBarVisibility プロパティと ComputedVerticalScrollBarVisibility プロパティの更新された値によって、ScrollBar 要素が表示されたり、表示されなくなったりします。


// 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 のドキュメント」を参照してください。

関連項目