How to determine when the XAML slider is complete?

In XAML, I have <Slider /> . It has a ValueChanged event. This event is fired every time the value changes. I need to determine when the value has changed. LostFocus, PointerReleased - wrong event. How can I detect this?

+6
source share
4 answers

You can create a new class and inherit from Slider. From there, you can find the Thumb control and listen to the events you need.

Something like this should work:

 public class SliderValueChangeCompletedEventArgs : RoutedEventArgs { private readonly double _value; public double Value { get { return _value; } } public SliderValueChangeCompletedEventArgs(double value) { _value = value; } } public delegate void SlideValueChangeCompletedEventHandler(object sender, SliderValueChangeCompletedEventArgs args); public class ExtendedSlider : Slider { public event SlideValueChangeCompletedEventHandler ValueChangeCompleted; private bool _dragging = false; protected void OnValueChangeCompleted(double value) { if (ValueChangeCompleted != null) { ValueChangeCompleted(this, new SliderValueChangeCompletedEventArgs(value) ); } } protected override void OnApplyTemplate() { base.OnApplyTemplate(); var thumb = base.GetTemplateChild("HorizontalThumb") as Thumb; if (thumb != null) { thumb.DragStarted += ThumbOnDragStarted; thumb.DragCompleted += ThumbOnDragCompleted; } thumb = base.GetTemplateChild("VerticalThumb") as Thumb; if (thumb != null) { thumb.DragStarted += ThumbOnDragStarted; thumb.DragCompleted += ThumbOnDragCompleted; } } private void ThumbOnDragCompleted(object sender, DragCompletedEventArgs e) { _dragging = false; OnValueChangeCompleted(this.Value); } private void ThumbOnDragStarted(object sender, DragStartedEventArgs e) { _dragging = true; } protected override void OnValueChanged(double oldValue, double newValue) { base.OnValueChanged(oldValue, newValue); if (!_dragging) { OnValueChangeCompleted(newValue); } } } 
+7
source

XAML, WinRT , Windows8.1 and UWP:

PointerCaptureLost event should work for mouse / touch
KeyUp event for keyboard

+8
source

I had a similar problem using Slider on Windows8 / WinRT.

My problem was this: I responded to a ValueChanged event and performed a lengthy operation (writing asynchronously to a file) after each trigger. And thus, it works with the simultaneous exception of editing. To avoid this, I used DispatcherTimer .

  //Class member private DispatcherTimer myDispatcherTimer = null; private void OnSliderValueChanged(object sender, RangeBaseValueChangedEventArgs e) { //I update my UI right away ... //If the dispatcher is already created, stop it if (myDispatcherTimer!= null) myDispatcherTimer.Stop(); //Overwrite the DispatcherTimer and thus reset the countdown myDispatcherTimer= new DispatcherTimer(); myDispatcherTimer.Tick += (sender, o) => DoSomethingAsync(); myDispatcherTimer.Interval = new TimeSpan(0,0,2); myDispatcherTimer.Start(); } private async void DoSomethingAsync() { await DoThatLongSaveOperation(); } 

You cannot directly determine the final value, but you can at least delay the operation until there is a long pause between the two updates (for example, in my case, if the user drags the slider and stops while saving the drag for 2 seconds, the save operation will be launched anyway).

+1
source

You can use a pair of values ​​bool isValueChanged and (if possible change the value without manipulating the pointer) isPressed;

 private void Slider_ValueChanged(object s, RangeBaseValueChangedEventArgs e) { if (!isPressed) { AcceptChanges(); } else { isValueChanged = true; } } 

Initialization Code:

 Window.Current.CoreWindow.PointerPressed += (e, a) => { isPressed = true; }; Window.Current.CoreWindow.PointerReleased += (e, a) => { isPressed = false; if (isValueChanged) AcceptChanges(); }; 
+1
source

All Articles