Scrollbar with a Fixed Thumb Size

I 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.

Here’s a SimpleStyle version of ScrollBar with fixed thumb.

3 Responses to “Scrollbar with a Fixed Thumb Size”

  1. Jeff Filla Says:

    I am unable to download the link you posted. It says the file has been moved or deleted. Could I get the example code for the fixed width thumb? Thanks

    - jeff

  2. notstatic.com Says:

    […] Not sure what happened, but the link for the Fixed Thumb Size Scrollbar Template (that’s a mouthful) that I had posted earlier doesn’t seem to work anymore, so here’s an update.  I’ll update the original post as well. […]

  3. Requisitos para Windows Presentation Foundation : Soft Obert Says:

    […] Kaxaml: Editor que acepta code snnipets y con una interfaz simple y agradable. […]

Leave a Reply