15
votes

I'm using a slider in a WPF window and I want that when the user clicks somewhere on the track of the slider, the thumb to go to that exact position. Currently, when I click somewhere, the thumb goes towards that position, but not to exactly that position. How can I achieve what I want ?

Edit: An example to better explain what I want: If the thumb is at 10 and I press the mouse down near 100 , I want it to jump to 100 (without passing through other values).

2

2 Answers

39
votes

you need to set IsMoveToPointEnabled to True: http://msdn.microsoft.com/en-us/library/system.windows.controls.slider.ismovetopointenabled.aspx

Slider.IsMoveToPointEnabled Gets or sets a value that indicates whether the Thumb of a Slider moves immediately to the location of the mouse click that occurs while the mouse pointer pauses on the Slider track.

4
votes

You should handle slider thumb mouse enter event and define behvior you want.

var thumb = (slider1.Template.FindName("PART_Track", slider) as Track).Thumb;
thumb.MouseEnter += new MouseEventHandler(ThumbMouseEnter);

Then you set position of the thumb in ThumbMouseEnter event hendler. THis will allow you to define any behavior you want.

Very similar question is asked on social.msdn.microsoft.com