This blog post answers a question on the WPF Forum which asks how to programmatically affect the text in a Slider’s auto tooltip.
Slider has a nice feature where it displays an “auto tooltip” as the user slides the thumb. The tooltip displays the current value of the Slider, and it follows the thumb as the user slides it back and forth. Slider exposes two properties that allow you to affect the auto tooltip, AutoToolTipPlacement and AutoToolTipPrecision. The latter allows you to affect how accurate the tooltip’s text is, relative to the Slider’s value. What is not possible, however, is to modify the text in the auto tooltip. This blog post shows one workaround for that problem.
I tried to gain access to the auto tooltip in many ways, but none succeeded. As a last resort, I decided to use a little reflection to gain access to the _autoToolTip field in Slider. After poking around in Reflector I realized that the auto tooltip’s content is only updated in two virtual methods: OnThumbDragStarted and OnThumbDragDelta. Armed with that information it was trivial to create a Slider subclass which makes it possible to format the auto tooltip’s display text.

Using the FormattedSlider is simply a matter of setting its AutoToolTipFormat property to a format string, as seen below:

The demo application looks like this when you run it:

Using a format string to modify the tooltip text is just one option. If you have more involved needs than just wrapping the Slider’s value in some text, you might want to change the AutoToolTipFormat property to a dependency property named something like AutoToolTipContent, which could then be set via triggers.
Download the demo project here: FormattedSlider (demo project) Be sure to change the extension from .DOC to .ZIP and then decompress it.
From http://joshsmithonwpf.wordpress.com/
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5