Display numbers to mark on WPF slider using TextBox

There have been several earlier questions related to adding numbers to ticks in WPF Slider

(e.g. here and here ). All of the suggested solutions I have found so far are about inheriting from a class TickBar

and then using DrawText(FormattedText text, Point point)

to draw the value. Something like:

protected override void OnRender(DrawingContext dc)
   foreach(double tick in Ticks)
      formattedText = new FormattedText(Convert.ToString(tick),
                                       new Typeface("Verdana"), // I don't know why all examples use the Verdana font :P
                                       10, Brushes.Black);
      dc.DrawText(formattedText, ComputeTheRightPosition(tick));


While this is a valid solution, I find that it is not common for me, because of FormattedText

. In my application, the parameter Style

can change at runtime, so I would prefer using all numbers in TextBlock

or something else that can dynamically update its style and font and that's it.

Is it possible to add textboxes this way to a subclass Slider

or TickBar



source to share

1 answer

From a ready-made solution -

Place the canvas above the slider. When the value of the slider changes, get the ratio of the value / (max-min)

Use the same ratio to position the text box on the canvas using SetLeft or Render Transform.



All Articles