Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The simplest way is to subclass Slider:</p> <pre><code>public class CustomSlider : Slider { public override void OnPreviewMouseMove(MouseEventArgs e) { if(e.LeftButton == MouseButtonState.Pressed) OnPreviewMouseLeftButtonDown(e); } } </code></pre> <p>In which case your XAML would be:</p> <pre><code>&lt;my:CustomSlider IsMoveToPointEnabled="True" /&gt; </code></pre> <p>For a more versatile solution that doesn't subclass Slider you can do it with an attached property:</p> <pre><code>public class SliderTools : DependencyObject { public static bool GetMoveToPointOnDrag(DependencyObject obj) { return (bool)obj.GetValue(MoveToPointOnDragProperty); } public static void SetMoveToPointOnDrag(DependencyObject obj, bool value) { obj.SetValue(MoveToPointOnDragProperty, value); } public static readonly DependencyProperty MoveToPointOnDragProperty = DependencyProperty.RegisterAttached("MoveToPointOnDrag", typeof(bool), typeof(SliderTools), new PropertyMetadata { PropertyChangedCallback = (obj, changeEvent) =&gt; { var slider = (Slider)obj; if((bool)changeEvent.NewValue) slider.MouseMove += (obj2, mouseEvent) =&gt; { if(mouseEvent.LeftButton == MouseButtonState.Pressed) slider.RaiseEvent(new MouseButtonEventArgs(mouseEvent.MouseDevice, mouseEvent.Timestamp, MouseButton.Left) { RoutedEvent = UIElement.PreviewMouseLeftButtonDownEvent, Source = mouseEvent.Source, }); }; } }); } </code></pre> <p>You would use this attached property on Slider along with the IsMoveToPointEnabled property:</p> <pre><code>&lt;Slider IsMoveToPointEnabled="True" my:SliderTools.MoveToPointOnDrag="True" ... /&gt; </code></pre> <p>Both of these solutions work by converting PreviewMouseMove events into equivalent PreviewMouseLeftButtonDown events whenever the left button is down.</p> <p>Note that the attached property does not remove the event handler when the property is set to false. I wrote it this way for simplicity since you almost never would need to remove such a handler. I recommend you stick with this simple solution, but if you want you can modify the PropertyChangedCallback to remove the handler when NewValue is false.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload