Search code examples
c#wpfscrollvieweronscrollchanged

What is the difference between the verticall offset of a ScrollViewer itself and that of ScrollChangedEventArgs


I set a ScrollChangedEventHandler for a ScrollViewer as shown in the code below.

private void scrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    var scrollViewer = sender as ScrollViewer;
    if (scrollViewer == null) 
        return; 

    Console.WriteLine(scrollViewer.VerticalOffset); 
    Console.WriteLine(e.VerticalOffset);
}

Sometimes, the 2 VerticalOffsets result in different values. Could you tell me what is the actual difference between the verticall offset of a ScrollViewer itself and that of ScrollChangedEventArgs?


Solution

  • As a conclusion from the documentation here and here for those two properties :

    ScrollViewer.VerticalOffset: represent the original value of the ScrollViewer Vertical offset
    (before you do the scroll, means the vertical offset before the event is triggered)

    e.VerticalOffset: represents the new updated value of the Vertical offset of the ScrollViewer
    (after you do the scroll, means the value of the vertical offset after the event is triggered)

    For general-purpose, you will use the e.VerticalOffset property.


    NOTE: the same is applied to the HorizontalOffset property.