C#WPF:滑块移动到准确位置

时间:2021-01-15 16:01:49

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 ?

我在WPF窗口中使用了一个滑块,当用户点击滑块轨道上的某个位置时,我想要那个拇指转到那个确切的位置。目前,当我点击某处时,拇指朝向该位置,但不是那个位置。我怎样才能实现我想要的目标?

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

编辑:一个更好地解释我想要的例子:如果拇指位于10并且我在100附近按下鼠标,我希望它跳到100(不通过其他值)。

2 个解决方案

#1


26  

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

你需要将IsMoveToPointEnabled设置为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.

Slider.IsMoveToPointEnabled获取或设置一个值,该值指示滑块的Thumb是否立即移动到鼠标指针在Slider轨道上暂停时发生的鼠标单击位置。

#2


3  

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.

然后在ThumbMouseEnter事件hendler中设置拇指的位置。这将允许您定义您想要的任何行为。

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

在social.msdn.microsoft.com上提出了非常类似的问题

#1


26  

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

你需要将IsMoveToPointEnabled设置为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.

Slider.IsMoveToPointEnabled获取或设置一个值,该值指示滑块的Thumb是否立即移动到鼠标指针在Slider轨道上暂停时发生的鼠标单击位置。

#2


3  

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.

然后在ThumbMouseEnter事件hendler中设置拇指的位置。这将允许您定义您想要的任何行为。

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

在social.msdn.microsoft.com上提出了非常类似的问题