WPF: Slider with an event that triggers after a user drags


I am currently making an MP3 player in WPF, and I want to make a slider that will allow the user to seek to a particular position in an MP3 by sliding the slider to the left or right.

I have tried using the ValueChanged event but that triggers every time it's value is changed, so if you drag it across, the event will fire multiple times, I want the event to only fire when the user has finished dragging the slider and Then get the new value.

How can I achieve this?


I have found this post on MSDN which basically discusses the same thing, and they came up with two "solutions"; either subclassing the Slider or invoking a DispatcherTimer in the ValueChanged event that invokes the action after a timespan.

Can you come up with anything better then the two mentioned above?

Best Solution

Besides using the Thumb.DragCompleted event you can also use both ValueChanged and Thumb.DragStarted, this way you don’t lose functionality when the user modifies the value by pressing the arrow keys or by clicking on the slider bar.


<Slider ValueChanged="Slider_ValueChanged"

Code behind:

private bool dragStarted = false;

private void Slider_DragCompleted(object sender, DragCompletedEventArgs e)
    this.dragStarted = false;

private void Slider_DragStarted(object sender, DragStartedEventArgs e)
    this.dragStarted = true;

private void Slider_ValueChanged(
    object sender,
    RoutedPropertyChangedEventArgs<double> e)
    if (!dragStarted)
Related Question