Scrollbar with a Fixed Thumb Size
Thursday, June 28th, 2007I thought I had posted this a while ago, but couldn’t find it today when someone asked me about it, so forgive me if this is a repeat.
You may have tried to do this, create a Scrollbar template where the thumb stays a fixed size, and then, like me, given up. At least that’s what I did the first time I tried it when Karsten and I were working on a demo for MIX last year.Â
Though not easily discoverable, it turns out that it is possible to do this. The trick is in the Track control (a very inner part of both the ScrollBar and Slider templates). Track has a tight relationship with ScrollViewer and even seeks out the ViewportSize property on a ScrollViewer on its own. That’s how it knows how to size the Thumb. If, however, you overrided the value for ViewportSize (on the Track, not the ScrollViewer) to be Double.NaN, then the track will leave the size of the Thumb alone.
In other words, deep in the guts of the ScrollBar template, you have a Track control. On that Track, you need to do this:
<Track ViewportSize=”{x:Static sys:Double.NaN} …
To get that to work, of course, you need to have mapped the System namespace to the prefix sys. You can do that like this:
xmlns:sys=”clr-namespace:System;assembly=mscorlib”
Once all that is done, Track will leave the size of the Thumb alone. At that point, make sure you give the Thumb a size, otherwise it will just disappear.