WPF implements slide control to change variable value after dragging

  • 2021-09-16 06:36:11
  • OfStack

WPF makes slide control drag to change the value of variables after the problem 1 began to feel quite simple, and many solutions have been seen on the Internet.

First of all, the simplest and most direct solution is to customize a dependency attribute named FinalValue. The OnThumbDragCompleted function is then overloaded, overwriting FinalValue when the Thumb control finishes dragging. 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;
    }
    }

Try a run and it seems to be done. Wait for 1 time, why don't the left and right keys work? It seems that there are still problems.

After 1, I found that I only need to rewrite the function of OnValue to achieve the function of left and right keys, but I can't rewrite this step. If I keep changing the value of FinalValue in OnValue, what's the difference between it and the original Slide? Therefore, OnThumbStart should be modified synchronously, and one flag bit should be set.

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);
        }
      }
    }
  }

One last point to note is that FinalValue needs to be written back to Value, and the position of Thumb essentially reflects the value of Value.


Related articles: