ScrollViewer の概要
ユーザー インターフェイス内のコンテンツは、多くの場合、コンピューター画面の表示領域よりも大きくなります。 ScrollViewer コントロールは、Windows Presentation Foundation (WPF) アプリケーションでコンテンツのスクロールを有効にする便利な方法を提供します。 このトピックでは、ScrollViewer 要素について説明し、いくつかの使用例を示します。
ScrollViewer コントロール
WPF アプリケーションでスクロールを有効にする定義済みの要素には、ScrollBar と ScrollViewerの 2 つがあります。 ScrollViewer コントロールは、水平および垂直の ScrollBar 要素とコンテンツ コンテナー (Panel 要素など) をカプセル化して、スクロール可能な領域に他の表示要素を表示します。 コンテンツのスクロールに ScrollBar 要素を使用するには、カスタム オブジェクトを作成する必要があります。 ただし、ScrollViewer 要素は、ScrollBar 機能をカプセル化する複合コントロールであるため、単独で使用できます。
ScrollViewer コントロールは、マウスとキーボードの両方のコマンドに応答し、コンテンツを事前に設定された増分でスクロールする多数のメソッドを定義します。 ScrollChanged イベントを使用して、ScrollViewer 状態の変化を検出できます。
ScrollViewer は子を 1 つだけ持つことができます。通常は、要素の Children コレクションをホストできる Panel 要素です。 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 要素は、必要な場合にのみ表示されます。 ウィンドウのサイズを変更すると、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の
関連項目
.NET Desktop feedback