Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop update value of slider while dragging it?

Tags:

wpf

slider

I've slider that its value is bind to some property, and the property updates it all the time. While dragging the [thumb on] slider I want to stop this update value of slider from binding, until user finish it's dragging.

Is there any property on Slider to do that, or I need to write code for that?

Thanks in advance!

like image 238
yossharel Avatar asked Nov 10 '10 16:11

yossharel


4 Answers

The template for Slider includes a Thumb, which raises the ThumbDragDelta event as the mouse is moved. Slider will always update the bound value immediately when it receives a ThumbDragDelta event.

The trick is to stop this event. The easiest way is to subclass Slider:

public class SliderIgnoreDelta : Slider
{
  protected override void OnThumbDragDelta(DragDeltaEventArgs e)
  {
    // Do nothing
  }
}

This slider will not update the value until the thumb drag completes.

Another solution is to intercept the ThumbDragDelta event on the Thumb. If you happen to be re-templating the Slider anyway, this might be a better solution. For example if you already have an EventBlocker class coded up that sets Handled true on the given RoutedEvent, you could put this in your template:

<Track.Thumb>
  <Thumb my:EventBlocker.EventToBlock="{x:Static Thumb.DragDeltaEvent}" />
</Track.Thumb>

But for most cases you'll probably want to go with my first solution.

like image 62
Ray Burns Avatar answered Oct 24 '22 14:10

Ray Burns


Hi this solution works for me. Just override Slider class and bind to FinalValue in xaml.

public class ExSlider : Slider
{
    public double FinalValue
    {
        get { return (double)GetValue(FinalValueProperty); }
        set { SetValue(FinalValueProperty, value); }
    }

    public static readonly DependencyProperty FinalValueProperty =
        DependencyProperty.Register(
            "FinalValue", typeof(double), typeof(ExSlider),
            new FrameworkPropertyMetadata(0d,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    protected override void OnThumbDragCompleted(DragCompletedEventArgs e)
    {
        base.OnThumbDragCompleted(e);
        FinalValue = Value;
    }

}
like image 22
Mirek Avatar answered Oct 24 '22 13:10

Mirek


I had the same issue so I made a simple slider extension as the next best thing. Adds two events to the slider:

ThumbDragStarted ThumbDragCompleted

public class SliderWithDraggingEvents : Slider
{
    public delegate void ThumbDragStartedHandler(object sender, DragStartedEventArgs e);
    public event ThumbDragStartedHandler ThumbDragStarted;


    public delegate void ThumbDragCompletedHandler(object sender, DragCompletedEventArgs e);
    public event ThumbDragCompletedHandler ThumbDragCompleted;

    protected override void OnThumbDragStarted(DragStartedEventArgs e)
    {
        if (ThumbDragStarted != null) ThumbDragStarted(this, e);
        base.OnThumbDragStarted(e);
    }

    protected override void OnThumbDragCompleted(DragCompletedEventArgs e)
    {
        if (ThumbDragCompleted != null) ThumbDragCompleted(this, e);
        base.OnThumbDragCompleted(e);
    }
}
like image 43
Dominic Avatar answered Oct 24 '22 14:10

Dominic


Added a flag to indicate dragging and only send out value changed events when the slider is being dragged.

 public class CustomSlider:Slider
{

    public bool IsDragging { get; protected set; }
    protected override void OnThumbDragCompleted(System.Windows.Controls.Primitives.DragCompletedEventArgs e)
    {
        IsDragging = false;
        base.OnThumbDragCompleted(e);

    }

    protected override void OnThumbDragStarted(System.Windows.Controls.Primitives.DragStartedEventArgs e)
    {
        IsDragging = true;
        base.OnThumbDragStarted(e);
    }

    protected override void OnValueChanged(double oldValue, double newValue)
    {
        if (!IsDragging)
        {
            base.OnValueChanged(oldValue, newValue);
        }
    }
}
like image 23
joshwl2003 Avatar answered Oct 24 '22 13:10

joshwl2003