C# WPF:滑块到精确位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18018920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:21:06  来源:igfitidea点击:

C# WPF : slider going to exact position

c#wpfuser-interfaceuser-controlsslider

提问by TheQuestioner

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 exactlythat 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 (不通过其他值)。

回答by Bolu

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

您需要设置IsMoveToPointEnabledTruehttp: //msdn.microsoft.com/en-us/library/system.windows.controls.slider.ismovetopointenabled.aspx

Slider.IsMoveToPointEnabledGets 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获取或设置一个值,该值指示当鼠标指针在 Slider 轨道上暂停时,Slider 的 Thumb 是否立即移动到鼠标单击的位置。

回答by skkap

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 事件处理程序中设置拇指的位置。这将允许您定义您想要的任何行为。

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

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