ScrollViewer コントロールは、コンテンツを水平方向または垂直方向にスクロールできるスクロール可能な領域を作成します。 ユーザー インターフェイス内のコンテンツは、多くの場合、コンピューター画面の表示領域よりも大きく、ScrollViewer は、Windows Presentation Foundation (WPF) アプリケーションでコンテンツのスクロールを有効にする便利な方法を提供します。
ScrollViewer コントロールは、水平方向と垂直方向のScrollBar要素とコンテンツ コンテナー (Panel要素など) をカプセル化して、スクロール可能な領域に他の表示要素を表示します。 ScrollViewer要素は、ScrollBar機能をカプセル化する複合コントロールであるため、単独で使用できます。
ScrollViewer コントロールは、マウスとキーボードの両方のコマンドに応答し、コンテンツを事前に設定された増分でスクロールする多数のメソッドを定義します。 ScrollChanged イベントを使用して、ScrollViewer状態の変化を検出できます。
ScrollViewerは子を 1 つだけ持つことができます。通常は、要素のPanel コレクションをホストできるChildren要素です。 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
Example
次の例では、テキストと四角形を含むウィンドウに ScrollViewer を作成します。 ScrollBar 要素は、必要な場合にのみ表示されます。 ウィンドウのサイズを変更すると、ScrollBarプロパティとComputedHorizontalScrollBarVisibilityプロパティの値が更新されたため、ComputedVerticalScrollBarVisibility要素が表示され、表示されなくなります。
// 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>
使い方に関するトピック
| Title | Description |
|---|---|
| ScrollChanged イベントを処理する | ScrollViewer コントロールで ScrollChanged イベントを処理する方法について説明します。 |
| IScrollInfo インターフェイスを使用してコンテンツをスクロールする | IScrollInfo インターフェイスを使用してコンテンツをスクロールする方法について説明します。 |
| ScrollViewer の Content-Scrolling メソッドを使用する | ScrollViewer のコンテンツ スクロール メソッドを使用する方法について説明します。 |
スタイルとテンプレート
このセクションでは、 ScrollViewer コントロールのスタイルとテンプレートについて説明します。 コントロールに一意の外観を与えるために、既定の ControlTemplate を変更できます。 詳細については、「 スタイルとテンプレートとは」 および 「コントロールのテンプレートを作成する方法」を参照してください。
コンテンツプロパティ
Content プロパティは、ScrollViewerの唯一の子を定義します。
部品
次の表に、 ScrollViewer コントロールの名前付きパーツを示します。
| 要素 | タイプ | Description |
|---|---|---|
| PART_HorizontalScrollBar | ScrollBar | ScrollBarコンテンツを水平方向にスクロールするために使用されます。 |
| PART_ScrollContentPresenter | ScrollContentPresenter | ScrollViewer内のコンテンツのプレースホルダー。 |
| PART_VerticalScrollBar(垂直スクロールバー) | ScrollBar | ScrollBarコンテンツを垂直方向にスクロールするために使用されます。 |
ビジュアル状態
次の表に、 ScrollViewer コントロールの表示状態を示します。
| VisualState 名称 | VisualStateGroupの名前 | Description |
|---|---|---|
| 無効フォーカス | 検証状態 |
Validation.HasError添付プロパティがtrueされ、コントロールにフォーカスがあります。 |
| フォーカスが外れた無効状態 | 検証状態 |
Validation.HasError添付プロパティがtrueであり、コントロールにフォーカスがありません。 |
| 有効 | 検証状態 | コントロールは Validation クラスを使用し、 Validation.HasError 添付プロパティが false。 |
ドキュメントのページ分割
ドキュメント コンテンツの場合、スクロールの代わりに、改ページ位置をサポートするドキュメント コンテナーを選択します。 FlowDocument は、複数のページにわたるコンテンツのページ分割をサポートし、スクロールの必要性を防ぐ、 FlowDocumentPageViewerなどの表示コントロール内でホストされるように設計されたドキュメント用です。 DocumentViewer は、従来のスクロールを使用して表示領域の領域外のコンテンツを表示する FixedDocument コンテンツを表示するためのソリューションを提供します。
ドキュメント形式とプレゼンテーション オプションの詳細については、「 WPF のドキュメント」を参照してください。
こちらも参照ください
.NET Desktop feedback