如何使WPF滑块后续光标从任意点 [英] How to make WPF Slider Thumb follow cursor from any point
本文介绍了如何使WPF滑块后续光标从任意点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的滑块:
<Slider Minimum="0" Maximum="100"
TickFrequency="1"
IsMoveToPointEnabled="True"
IsSnapToTickEnabled="False"/>
我要让下一个行为:
时的MouseDown发生在滑块的任何一点,拇指不仅移动一次到这一点,也是继光标直至发生的MouseUp
I want to make next behavior: when MouseDown occured at any point of slider, Thumb not only moves once to that point, but also following cursor until MouseUp occurs.
很抱歉,如果有人问了,我找不到这个问题。
Sorry if it was asked already, i couldn't find that question.
推荐答案
此行为会发生的仅当你拖动滑块本身,它是由设计。
This behavior will happen only when you drag the slider thumb itself, it is by design.
的然而的,这里的一些代码,将做到这一点; - )
However, here's some code that will do it ;-)
勾MouseMove事件在XAML:
Hook to the MouseMove event in XAML:
<Slider MouseMove="Slider_OnMouseMove" IsMoveToPointEnabled="True"/>
然后插入代码:
Then insert this code:
private void Slider_OnMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var slider = (Slider)sender;
Point position = e.GetPosition(slider);
double d = 1.0d / slider.ActualWidth * position.X;
var p = slider.Maximum * d;
slider.Value = p;
}
}
请注意:
- 您可能有他们应该有所不同把你的滑块的边距和填充进去,我没有。
- 这样做对一个水平滑块。 我的滑块
- 最小值为0,一定要调整计算应您的最低是否定的。
- 最后,它似乎只有当IsMoveToPointEnabled设置工作。
- You might have to take margins and padding of your slider into account should they differ, I haven't.
- This was done on an horizontal slider.
- Minimum value of my slider was 0, make sure to adjust the calculation should your minimum be negative.
- Finally, it does seem to work only when IsMoveToPointEnabled is set.
这篇关于如何使WPF滑块后续光标从任意点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文