locked
In what unit is the ScrollViewer.VerticalOffset? RRS feed

Answers

  • Depends on the panel you use. For non-virtualizing panels it is DIPs. For virtualizing panels it is in items.
    • Proposed as answer by NokItOff Friday, September 7, 2012 8:28 PM
    • Marked as answer by FrodeNilsen2 Wednesday, September 12, 2012 7:46 AM
    Friday, September 7, 2012 6:43 PM

All replies

  • Yeah, it's odd it's a double. I would have expected it to be Int32 and be 1 unit per pixel.

    Maybe play around with it...e.g. if you put in 50, does it scroll 50 pixels, or 50%?

    Friday, September 7, 2012 2:33 PM
  • VerticalOffset should be measured in device-independent pixels. If you set it to 50, then it should scroll down 50 pixel units.
    Friday, September 7, 2012 3:33 PM
  • It's not device-independent pixels. In my solution, the ActualHeight property is 485 while the VerticalOffset is in the range of 2 to 12. The value of ExtentHeight is 12 though, so I've found this formula to work in my case, although I'm far from sure this will work in every case:

    var deviceIndependentPixelsScrolled = (sv.VerticalOffset-2) / sv.ExtentHeight * sv.ActualHeight

    Update: No that formula was not accurate either. If I swap ExtentHeight with ViewportHeight, I'm closer but still not quite there. The weird thing is that the width seems to be in device-independent pixels though. Is this whole thing a bug?

    Friday, September 7, 2012 4:06 PM
  • What value does ViewportHeight have? Also, are you doing any zooming?
    Friday, September 7, 2012 4:46 PM
  • Zooming would probably explain it, but I'm not zooming. Here's the dump from the output of the immediate window, giving ViewportHeight and more:

    {Windows.UI.Xaml.Controls.ScrollViewer}
    base {Windows.UI.Xaml.Controls.ContentControl}: {Windows.UI.Xaml.Controls.ScrollViewer} ComputedHorizontalScrollBarVisibility: Collapsed ComputedVerticalScrollBarVisibility: Visible ExtentHeight: 22.0 ExtentWidth: 590.0 HorizontalOffset: 0.0 HorizontalScrollBarVisibility: Disabled HorizontalScrollMode: Disabled HorizontalSnapPointsAlignment: Near HorizontalSnapPointsType: Optional IsHorizontalRailEnabled: false IsHorizontalScrollChainingEnabled: true IsScrollInertiaEnabled: true IsVerticalRailEnabled: false IsVerticalScrollChainingEnabled: true IsZoomChainingEnabled: true IsZoomInertiaEnabled: true MaxZoomFactor: 10.0 MinZoomFactor: 0.1 ScrollableHeight: 12.300000190734863 ScrollableWidth: 0.0 VerticalOffset: 11.508070916458587 VerticalScrollBarVisibility: Auto VerticalScrollMode: Enabled VerticalSnapPointsAlignment: Near VerticalSnapPointsType: Optional ViewportHeight: 9.6999998092651367 ViewportWidth: 590.0 ZoomFactor: 1.0 ZoomMode: Disabled ZoomSnapPoints: Windows Runtime Object ZoomSnapPointsType: Optional

    By the way, this is a ScrollViewer-object that is part of the ListView control. I've acquired it through the VisualTreeHelper. Don't know if that is relevant.

    Friday, September 7, 2012 5:16 PM
  • Depends on the panel you use. For non-virtualizing panels it is DIPs. For virtualizing panels it is in items.
    • Proposed as answer by NokItOff Friday, September 7, 2012 8:28 PM
    • Marked as answer by FrodeNilsen2 Wednesday, September 12, 2012 7:46 AM
    Friday, September 7, 2012 6:43 PM
  • In ListView controls, the offset is in fractions of the height of the ListViewItems contained within.  You'll either need to use APIs like TransformToVisual (tricky in virtualized scenarios) or estimate using the height if your ListViewItem's template plus the padding/margin inherent in the default ListViewItem style.

    XAML SDET Lead : Input / DirectManipulation / Accessibility

    • Proposed as answer by NokItOff Friday, September 7, 2012 8:28 PM
    Friday, September 7, 2012 6:54 PM
  • Matt: Why do you say "estimate using the height of yourListViewItem's template (...)" - wouldn't that procedure yield a precise value?

    For the record, I ended up solving my problem from a different angle where I didn't have to do this calculation, but I think I was close.

    Wednesday, September 12, 2012 7:51 AM
  • Hi FrodeNilsen2,

    What was the other approach that you found? I have a virtualized list view and items need not be of same size. I need zoom operation as well. I need to know after i scroll, what is the currently visible region of the scroll view? I got the scroll view from the list view template.

    Any suggestions?

    Monday, October 15, 2012 2:12 PM
  • Matt, can you please provide an example for the calculation for virtualized scenario with variable size elements?
    Tuesday, October 16, 2012 5:51 AM
  • Matt - Can you please explain how to calculate the fractions that you mentioned as "the offset is in fractions of the height of the ListViewItems contained within". Then it would be easy to scroll to the calculated vertical offset.


    - Ram

    Saturday, October 20, 2012 7:26 PM
  • Matt - Can you please explain how to calculate the fractions that you mentioned as "the offset is in fractions of the height of the ListViewItems contained within". Then it would be easy to scroll to the calculated vertical offset.


    - Ram

    I never actually managed to do that properly. I ended up solving my problem with a different approach not depending on how to calculate the virtual pixels and scrolling. My post from Friday, September 07, 2012 4:06 PM is still the closest I got to a solution, I'm afraid.

    edit: I realize you asked Matt, but anyway.. 
    Monday, October 22, 2012 9:28 AM