0

I need to create a custom Scrollbar in UWP ? But I can't find any clear logic to calculate Scrollbar thumb width and Position. What I have tried ?

ThumbWidth=(VisibleLength/TotalLength)*ScrollRegionWidth;
ThumbPosition=(ScrollLeft/TotalLength)*ScrollRegionWidth;

where,

VisibleLength -> viewable area width  
TotalLength -> Total Content width  
ScrollRegionWidth -> Scrollbar width - (2 x ArrowWidth)  (or) Thumb Track width
ScrollLeft -> how much content scrolled !! 

Is it correct ? I tried like this . But the scrollbar thumb won't goes to end of thumb track while my whole content is scrolled to end !

I need to create a custom Scrollbar and how to do it ? I need complete logic for thumb width and position .

1 Answer 1

1

How to Create a Custom Scrollbar in UWP?

ThumbWidth=(VisibleLength/TotalLength)*ScrollRegionWidth;

Your first line is correct. and you could verify it with VisibleLength = TotalLength, then ThumbWidth will fill all the ScrollRegion.

ThumbPosition=(ScrollLeft/TotalLength)*ScrollRegionWidth;

But the second line looks not correct, Thumb's start position should be left top, the ScrollRegion should be (ScrollRegionWidth - ThumbWidth).

ThumbPosition=(ScrollLeft/TotalLength)*(ScrollRegionWidth-ThumbWidth);
9
  • How to calculate the step to be scrolled if 1px of thumb is moved ? Commented Feb 10, 2022 at 5:07
  • It's math problem, ScrollLeft = (ThumbPosition * TotalLength)/(ScrollRegionWidth-ThumbWidth)
    – Nico Zhu
    Commented Feb 10, 2022 at 5:30
  • And i have one more question, if scrollable Content(Total Length) was high ,the scrollThumb width gets very small . So i'm going to set ScrollThumb Minimum width as 20 .In this case how to scroll content ? Commented Feb 10, 2022 at 5:30
  • Base on this ScrollLeft = (ThumbPosition * TotalLength)/(ScrollRegionWidth-ThumbWidth) if TotalLength more than threshold value, we could set ThumbWidth as fix value 20. it will not effect ScrollLeft value right?
    – Nico Zhu
    Commented Feb 10, 2022 at 5:37
  • 1
    ThumbWidth calculation was Right. ThumbPosition should be ThumbPosition=(ScrollLeft/TotalLength)*(ScrollRegionWidth-(ThumbWidth-ActualThumbWidth)). We need to cache the ActualThumbWidth for the case to maintain the minimum ThumbWidth. It works perfect for me. Commented Feb 11, 2022 at 2:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.