I'm just trying to figure out a way to get the thumb of a slider in WPF, something like so:
Slider mySlider = new Slider();
Thumb thumb = slider.Thumb;
Now I know it's not possible as simple as this; but there must be a work around this. Let me know if you know any.
Thanks!
If you just want to add an event handler to the thumb and don't want to wait or force rendering to avoid
slider.Template
beingnull
, you can do something like this:Found the solution on my own, with a little help from the
VisualTreeHelper
. Any optimization greatly appreciated:Slider
has aTemplatePartAttribute
that declares it should have a template part calledPART_Track
of typeTrack
. TheTrack
can give us a reference to theThumb
. Keep in mind that it's possible to give theSlider
a template with noTrack
, in which case there will be noThumb
.