Partager via


Vue d’ensemble de ScrollViewer

Le contenu d’une interface utilisateur est souvent plus grand que la zone d’affichage d’un écran d’ordinateur. Le contrôle ScrollViewer 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 contrôle ScrollViewer encapsule les éléments ScrollBar horizontaux et verticaux et un conteneur de contenu (tel qu’un élément Panel) afin d’afficher d’autres éléments visibles dans une zone de défilement. Vous devez créer un objet personnalisé pour utiliser l’élément ScrollBar pour le 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 la fonctionnalité de ScrollBar.

Le contrôle ScrollViewer 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 état ScrollViewer.

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

Défilement physique vs. logique

Le défilement physique est utilisé pour faire défiler le contenu par incrément physique prédéterminé, généralement par une valeur déclarée en pixels. Le défilement logique est utilisé pour faire défiler 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 éléments Panel. 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 contrôle ScrollViewer 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 éléments qui nécessitent le défilement par unité logique, plutôt que par incrément physique. Effectuer un cast d’une instance de IScrollInfo en un contrôle Panel dérivé et utiliser ensuite ses méthodes de défilement constitue un moyen utile pour faire défiler l’écran jusqu’à l’unité logique suivante dans une collection enfant plutôt que selon un incrément de pixels. Par défaut, le contrôle ScrollViewer prend en charge le 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 Panel hôte dans un ScrollViewer et en définissant la propriété CanContentScroll sur false.

L’exemple de code suivant montre comment convertir une instance de IScrollInfo en un 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 dans une fenêtre qui contient du texte et un rectangle. ScrollBar éléments s’affichent uniquement lorsqu’ils sont nécessaires. Lorsque vous redimensionnez la fenêtre, les éléments ScrollBar apparaissent et disparaissent, en raison des valeurs mises à jour des propriétés ComputedHorizontalScrollBarVisibility et 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>

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

Comme tous les contrôles dans Windows Presentation Foundation, le 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 du document, une alternative au défilement consiste à choisir un conteneur de documents qui prend en charge la pagination. FlowDocument concerne les 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 visualiser le contenu FixedDocument, qui utilise le défilement traditionnel pour afficher le contenu en dehors de la zone d’affichage.

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

Voir aussi