简体   繁体   English

如何获取scrollviewer中可拖动栏的高度?

[英]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 ? 另一个问题:scrollViewer.VerticalOffset的测量单位是scrollViewer.VerticalOffset If it isn't in pixel what cast should i do? 如果不是像素,该怎么办?

You can calculate that value from the IScrollInfo Interface . 您可以从IScrollInfo接口计算该值。 The ScrollViewer Class implements this interface and exposes the relevant properties that you need to use. ScrollViewer实现此接口,并公开您需要使用的相关属性。 As far as I can remember, you need to utilise three properties: 据我所记得,您需要利用三个属性:

ExtentHeight - Gets a value that contains the vertical size of the extent. ExtentHeight获取一个值,该值包含范围的垂直大小。

ViewportHeight - Gets a value that contains the vertical size of the content's viewport. ViewportHeight获取一个包含内容视口的垂直大小的值。

VerticalOffset - Gets a value that contains the vertical offset of the scrolled content. VerticalOffset获取一个值,该值包含滚动内容的垂直偏移量。

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. 为了进一步说明, viewportScrollViewer的可见区域有关,而Extent表示可滚动区域的总大小。 The VerticalOffset describes the amount that the scrollable content has been scrolled. VerticalOffset描述了可滚动内容已滚动的数量。 Using these three values, you should be able to calculate your required values that relate to the ScrollBar . 使用这三个值,您应该能够计算出与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. 正是由于这个原因,Microsoft公开了许多通用属性。 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: 就您而言,我相信您可以利用SystemParameters.VerticalScrollBarButtonHeight属性 ,尽管您可能需要添加一些内容来容纳Padding和/或Margin值:

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. 你知道...我只是想的东西......你甚至可以让你的拇指需要Height从这些SystemParameters ...你可以尝试SystemParameters.VerticalScrollBarThumbHeight属性 ,虽然我不认为它会使用自定义ScrollBar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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