如何使WPF Slider Thumb从任何点跟随光标

时间:2022-12-22 16:03:58

I have such slider:

我有这样的滑块:

<Slider Minimum="0" Maximum="100" 
TickFrequency="1"
IsMoveToPointEnabled="True"
IsSnapToTickEnabled="False"/>

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.

我想做下一个行为:当MouseDown在滑块的任何一点出现时,Thumb不仅会移动一次到该点,而且还会跟随光标直到MouseUp发生。

Sorry if it was asked already, i couldn't find that question.

对不起,如果已经问过,我找不到那个问题。

2 个解决方案

#1


3  

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

但是,这里有一些代码可以做到;-)

Hook to the MouseMove event in XAML:

挂钩到XAML中的MouseMove事件:

<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;
    }
}

Note :

注意 :

  • 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.
  • 我的滑块的最小值为0,如果最小值为负,请务必调整计算。
  • Finally, it does seem to work only when IsMoveToPointEnabled is set.
  • 最后,它似乎只在IsMoveToPointEnabled设置时才有效。

#2


1  

Aybe's solution works better in the thumb_MouseMove event.

Aybe的解决方案在thumb_MouseMove事件中效果更好。

void timelineThumb_MouseMove(object sender, MouseEventArgs e)
{

     if (e.LeftButton == MouseButtonState.Pressed)
     {
         var thumb = sender as Thumb;

         Point pos = e.GetPosition(uiVideoPlayerTimelineSlider);
         double d = 1.0d / uiVideoPlayerTimelineSlider.ActualWidth * pos.X;
         uiVideoPlayerTimelineSlider.Value = uiVideoPlayerTimelineSlider.Maximum * d;

     }
}

#1


3  

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

但是,这里有一些代码可以做到;-)

Hook to the MouseMove event in XAML:

挂钩到XAML中的MouseMove事件:

<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;
    }
}

Note :

注意 :

  • 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.
  • 我的滑块的最小值为0,如果最小值为负,请务必调整计算。
  • Finally, it does seem to work only when IsMoveToPointEnabled is set.
  • 最后,它似乎只在IsMoveToPointEnabled设置时才有效。

#2


1  

Aybe's solution works better in the thumb_MouseMove event.

Aybe的解决方案在thumb_MouseMove事件中效果更好。

void timelineThumb_MouseMove(object sender, MouseEventArgs e)
{

     if (e.LeftButton == MouseButtonState.Pressed)
     {
         var thumb = sender as Thumb;

         Point pos = e.GetPosition(uiVideoPlayerTimelineSlider);
         double d = 1.0d / uiVideoPlayerTimelineSlider.ActualWidth * pos.X;
         uiVideoPlayerTimelineSlider.Value = uiVideoPlayerTimelineSlider.Maximum * d;

     }
}