Partager via


Comment : déclencher l'événement ScrollChanged

Mise à jour : novembre 2007

Exemple

Cet exemple montre comment déclencher l'événement ScrollChanged d'un ScrollViewer.

Un élément FlowDocument avec quatre paragraphes https://msdn.microsoft.com/fr-fr/library/ms522790(v=vs.90) comme défini en XAML (Extensible Application Markup Language). Le FlowDocument réside dans un parent ScrollViewer, qui garantit qu'une barre de défilement verticale sera visible. Lorsque l'événement ScrollChanged se produit, une méthode personnalisée définie dans C# est déclenchée, et le texte est écrit dans un TextBlock qui indique que l'événement s'est produit.

L'exemple de code suivant est abrégé. Pour l'exemple complet, consultez Événement ScrollChanged, exemple.

<ScrollViewer Name="sv1" CanContentScroll="False" ScrollChanged="sChanged">

  <FlowDocument FontFamily="Arial" PageWidth="400">
    <Paragraph>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
      laoreet dolore magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis nostrud exerci tation
      ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
      laoreet dolore magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis nostrud exerci tation
      ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
      laoreet dolore magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis nostrud exerci tation
      ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
    </Paragraph>

...

    <Paragraph>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
      laoreet dolore magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis nostrud exerci tation
      ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
      laoreet dolore magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis nostrud exerci tation
      ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
      laoreet dolore magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis nostrud exerci tation
      ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
    </Paragraph>

  </FlowDocument>
</ScrollViewer>

L'événement ScrollChanged est géré dans un fichier code-behind C#. Lorsque l'événement ScrollChanged se produit, un MessageBox affiche un message l'indiquant à l'utilisateur.

Private Sub sChanged(ByVal sender As Object, ByVal args As ScrollChangedEventArgs)


    If (sv1.CanContentScroll = True) Then
        tBlock1.Foreground = System.Windows.Media.Brushes.Red
        tBlock1.Text = "ScrollChangedEvent just Occurred"
        tBlock3.Text = "ExtentWidth is now " + args.ExtentWidth.ToString()
        tBlock4.Text = "ExtentHeightChange was " + args.ExtentHeightChange.ToString()
        tBlock5.Text = "ExtentWidthChange was " + args.ExtentWidthChange.ToString()
        tBlock6.Text = "HorizontalOffset is now " + args.HorizontalOffset.ToString()
        tBlock7.Text = "VerticalOffset is now " + args.VerticalOffset.ToString()
        tBlock8.Text = "HorizontalChange was " + args.HorizontalChange.ToString()
        tBlock9.Text = "VerticalChange was " + args.VerticalChange.ToString()
        tBlock10.Text = "ViewportHeight is now " + args.ViewportHeight.ToString()
        tBlock11.Text = "ViewportWidth is now " + args.ViewportWidth.ToString()
        tBlock12.Text = "ViewportHeightChange was " + args.ViewportHeightChange.ToString()
        tBlock13.Text = "ViewportWidthChange was " + args.ViewportWidthChange.ToString()

    Else
        tBlock1.Text = ""

    End If
private void sChanged(object sender, ScrollChangedEventArgs e)
{
    if (sv1.CanContentScroll == true)
    {
        tBlock1.Foreground = System.Windows.Media.Brushes.Red;
        tBlock1.Text = "ScrollChangedEvent just Occurred";
        tBlock2.Text = "ExtentHeight is now " + e.ExtentHeight.ToString();
        tBlock3.Text = "ExtentWidth is now " + e.ExtentWidth.ToString();
        tBlock4.Text = "ExtentHeightChange was " + e.ExtentHeightChange.ToString();
        tBlock5.Text = "ExtentWidthChange was " + e.ExtentWidthChange.ToString();
        tBlock6.Text = "HorizontalOffset is now " + e.HorizontalOffset.ToString();
        tBlock7.Text = "VerticalOffset is now " + e.VerticalOffset.ToString();
        tBlock8.Text = "HorizontalChange was " + e.HorizontalChange.ToString();
        tBlock9.Text = "VerticalChange was " + e.VerticalChange.ToString();
        tBlock10.Text = "ViewportHeight is now " + e.ViewportHeight.ToString();
        tBlock11.Text = "ViewportWidth is now " + e.ViewportWidth.ToString();
        tBlock12.Text = "ViewportHeightChange was " + e.ViewportHeightChange.ToString();
        tBlock13.Text = "ViewportWidthChange was " + e.ViewportWidthChange.ToString();
    }
    else
    {
        tBlock1.Text = "";
    }

Voir aussi

Référence

ScrollViewer

ScrollChanged

ScrollChangedEventHandler

ScrollChangedEventArgs