The slide control of WPF is dragged to change the variable value.
In WPF, changing the variable value after dragging the slide control is quite simple at first, and there are many solutions on the Internet.
First, the simplest and most direct solution is to customize a dependency attribute named FinalValue. Then the OnThumbDragCompleted function is reloaded, And the FinalValue is rewritten when the Thumb control is dragged. The Code is as follows:
public class SliderIgnoreDelta : Slider { public int FinalValue { get { return (int)GetValue(FinalValueProperty); } set { SetValue(FinalValueProperty, value); } } public static readonly DependencyProperty FinalValueProperty = DependencyProperty.Register( "FinalValue", typeof(int), typeof(SliderIgnoreDelta), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFinalValueChanged)); private static void OnFinalValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { int result; if (int.TryParse(e.NewValue.ToString(), out result)) { if (((SliderIgnoreDelta) sender).Value != result) { ((SliderIgnoreDelta) sender).Value = result; } } } protected override void OnThumbDragCompleted(System.Windows.Controls.Primitives.DragCompletedEventArgs e) { base.OnThumbDragCompleted(e); FinalValue = (int)Value; } }
I tried to run it. It seems I have done it. Wait, why does the left and right keys not work. It seems that there are still problems.
After a while, I found that only the OnValue function needs to be rewritten to implement the left and right keys, but this step is not enough, what is the difference between changing the FinalValue in OnValue and the original Slide? Therefore, you must modify OnThumbStart synchronously and set a flag.
The final code is as follows:
public class SliderIgnoreDelta : Slider { public int FinalValue { get { return (int)GetValue(FinalValueProperty); } set { SetValue(FinalValueProperty, value); } } public static readonly DependencyProperty FinalValueProperty = DependencyProperty.Register( "FinalValue", typeof(int), typeof(SliderIgnoreDelta), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFinalValueChanged)); private static void OnFinalValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { int result; if (int.TryParse(e.NewValue.ToString(), out result)) { if (((SliderIgnoreDelta) sender).Value != result) { ((SliderIgnoreDelta) sender).Value = result; } } } public bool IsDragging { get; protected set; } protected override void OnThumbDragCompleted(System.Windows.Controls.Primitives.DragCompletedEventArgs e) { IsDragging = false; base.OnThumbDragCompleted(e); OnValueChanged(Value, Value); } 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); if (FinalValue != (int)Math.Round(Value, 0)) { FinalValue = (int)Math.Round(Value, 0); } } } }
Note that FinalValue needs to be written back to Value. The location of Thumb actually reflects the Value.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.