简体   繁体   中英

How to get the height of the draggable bar in a scrollviewer?

I am trying to get the distance in pixel between the top of the control and the middle of my vertical scrollbar (not all the scrollviewer, only the bar you can drag to scroll the control). I don't understand which property i should use. This is the code i wrote:

double barHeight = /*to do*/;
double barUpperEdge = scrollViewer.VerticalOffset;

double distance = barUpperEdge  + (barHeight/2);

Another question: which is the mesurament unit of scrollViewer.VerticalOffset ? If it isn't in pixel what cast should i do?

You can calculate that value from the IScrollInfo Interface . The ScrollViewer Class implements this interface and exposes the relevant properties that you need to use. As far as I can remember, you need to utilise three properties:

ExtentHeight - Gets a value that contains the vertical size of the extent.

ViewportHeight - Gets a value that contains the vertical size of the content's viewport.

VerticalOffset - Gets a value that contains the vertical offset of the scrolled content.

To explain a little further, the viewport relates to the visible area of the ScrollViewer and the Extent represents the total size of the scrollable area. The VerticalOffset describes the amount that the scrollable content has been scrolled. Using these three values, you should be able to calculate your required values that relate to the ScrollBar . Try something like this:

double barHeight = ViewportHeight * scrollviewer.ActualHeight / ExtentHeight;

UPDATE >>>

Please note that it is generally bad practice to use constant values in your calculations. Microsoft have exposed many common properties for this very reason. In your case, I believe that you can make use of the SystemParameters.VerticalScrollBarButtonHeight property , although you may need to add something to accommodate Padding and/or Margin values:

double barHeight = ViewportHeight * (scrollviewer.ActualHeight -  2 * 
    SystemParameters.VerticalScrollBarButtonHeight) / ExtentHeight;

You know... I've just thought of something... you may even be able to get your required thumb Height from these SystemParameters ... you could try the SystemParameters.VerticalScrollBarThumbHeight Property , although I don't think that it will work with custom ScrollBar s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM