Vue d’ensemble de ScrollViewer

Le contenu d’une interface utilisateur occupe souvent un espace plus important que la zone d’affiche d’un écran d’ordinateur. Le ScrollViewer contrôle offre un moyen pratique d’activer le défilement de contenu dans les applications WPF (Windows Presentation Foundation). Cette rubrique présente l’élément ScrollViewer et fournit plusieurs exemples d’utilisation.

Le contrôle ScrollViewer

Il existe deux éléments prédéfinis qui activent le défilement dans les applications WPF : ScrollBar et ScrollViewer. Le ScrollViewer contrôle encapsule les éléments horizontaux et verticaux ScrollBar et un conteneur de contenu (tel qu’un Panel élément) afin d’afficher d’autres éléments visibles dans une zone de défilement. Vous devez créer un objet personnalisé afin d’utiliser l’élément pour le ScrollBar défilement du contenu. Toutefois, vous pouvez utiliser l’élément ScrollViewer lui-même, car il s’agit d’un contrôle composite qui encapsule les ScrollBar fonctionnalités.

Le ScrollViewer contrôle répond aux commandes souris et clavier, et définit de nombreuses méthodes avec lesquelles faire défiler le contenu par incréments prédéterminés. Vous pouvez utiliser l’événement ScrollChanged pour détecter une modification dans un ScrollViewer état.

Un ScrollViewer seul enfant peut contenir un seul enfant, généralement un Panel élément qui peut héberger une Children collection d’éléments. La Content propriété définit le seul enfant du ScrollViewer.

Défilement physique ou logique

Le défilement physique est utilisé pour faire défiler le contenu selon un incrément physique prédéterminé, généralement une valeur déclarée en pixels. Le défilement logique est utilisé pour faire défiler l’écran jusqu’à l’élément suivant dans l’arborescence logique. Le défilement physique est le comportement de défilement par défaut pour la plupart des Panel éléments. WPF prend en charge les deux types de défilement.

L’interface IScrollInfo

L’interface IScrollInfo représente la région de défilement principale au sein d’un ScrollViewer contrôle ou dérivé. L’interface définit les propriétés et méthodes de défilement qui peuvent être implémentées par Panel des éléments qui nécessitent le défilement par unité logique, plutôt que par incrément physique. La conversion d’une instance vers IScrollInfo une instance dérivée Panel , puis l’utilisation de ses méthodes de défilement fournit un moyen utile de faire défiler vers l’unité logique suivante dans une collection enfant, plutôt que par incrément de pixels. Par défaut, le contrôle prend en charge le ScrollViewer défilement par unités physiques.

StackPanel et VirtualizingStackPanel implémentent IScrollInfo et prennent en charge le défilement logique en mode natif. Pour les contrôles de disposition qui prennent en charge le défilement logique en mode natif, vous pouvez toujours obtenir le défilement physique en encapsulant l’élément hôte Panel dans un ScrollViewer et en définissant la CanContentScroll propriété falsesur .

L’exemple de code suivant montre comment convertir une instance d’une instance en IScrollInfo une StackPanel et utiliser des méthodes de défilement de contenu (LineUp et LineDown) définies par l’interface.

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

Définition et utilisation d’un élément ScrollViewer

L’exemple suivant crée une ScrollViewer fenêtre qui contient du texte et un rectangle. ScrollBar les éléments apparaissent uniquement lorsqu’ils sont nécessaires. Lorsque vous redimensionnez la fenêtre, les ScrollBar éléments apparaissent et disparaissent, en raison de valeurs mises à jour des propriétés et ComputedVerticalScrollBarVisibility des ComputedHorizontalScrollBarVisibility propriétés.


// 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>

Application d’un style à un élément ScrollViewer

Comme tous les contrôles dans Windows Presentation Foundation, il ScrollViewer peut être mis en forme pour modifier le comportement de rendu par défaut du contrôle. Pour plus d’informations sur l’application d’un style aux contrôles, consultez Application d’un style et création de modèles.

Pagination de documents

Pour le contenu de document, il est possible de choisir un conteneur de documents qui prend en charge la pagination plutôt que d’utiliser le défilement. FlowDocument est destiné aux documents conçus pour être hébergés dans un contrôle d’affichage, tel que FlowDocumentPageViewer, qui prend en charge la pagination du contenu sur plusieurs pages, ce qui empêche le défilement. DocumentViewer fournit une solution pour afficher FixedDocument le contenu, qui utilise le défilement traditionnel pour afficher le contenu en dehors du domaine de l’affichage.

Pour plus d’informations sur les formats et les options de présentation de documents, consultez Documents dans WPF.

Voir aussi