The slide control of WPF is dragged to change the variable value.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.