About Slider?

尼龟 杰 190 Reputation points
2023-06-22T04:58:08.2633333+00:00

Look at![RTNXM0Y2TI2L]FVY$3RUYI9](https://learn.microsoft.com/api/attachments/e4a602f6-a689-4fda-807f-2e6ab9327563?platform=QnA)

1

if I click the area of 'a',the thumb would 'jump' to my point and can only one forward 'jump',

I want to:

It would directly go at my point and can smoothly move towards all directions if I still Press the left button.

Thanks for any answers.

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2023-06-23T06:41:35.2833333+00:00

    Hi,@尼龟 杰.I have not been able to successfully reproduce your environment with your code. You could refer to the code below to see if it meets your needs.

    Xaml:

      <Slider x:Name="slider"
                    PreviewMouseLeftButtonDown="Slider_PreviewMouseLeftButtonDown"
                    PreviewMouseMove="Slider_PreviewMouseMove"
                    PreviewMouseLeftButtonUp="Slider_PreviewMouseLeftButtonUp" />
    
    
    

    Codebedhind:

       
            private bool isDragging;
            private void Slider_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                slider.CaptureMouse();
                double mousePosition = e.GetPosition(slider).X;
                double value = mousePosition / slider.ActualWidth * (slider.Maximum - slider.Minimum) + slider.Minimum;
                slider.Value = value;
                isDragging = true;
    
            }
    
            private void Slider_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                if (isDragging)
                {
                    double mousePosition = e.GetPosition(slider).X;
                    double value = mousePosition / slider.ActualWidth * (slider.Maximum - slider.Minimum) + slider.Minimum;
                    slider.Value = value;
                }
            }
    
            private void Slider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                if (isDragging)
                {
                    slider.ReleaseMouseCapture();
                    isDragging = false;
                }
            }
    

    The result:

    8


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. 尼龟 杰 190 Reputation points
    2023-06-22T05:43:26.5066667+00:00

    Thanks,I've solve it by myself,but I think my solution is too heavy,I still wanna others.

    bool hc = false;         
    private void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)         
    {             
    	hc = Mouse.Capture(sender as Border);
        Border father = (sender as Border).Tag as Border;
        double percent = e.GetPosition(father).X / father.ActualWidth;
        Value = Maximum * percent;         
    
    }         
    private void border_MouseMove(object sender, MouseEventArgs e)         
    {             
    	if (!hc)                 
    		return;             
    	Border father = (sender as Border).Tag as Border;
        double percent = e.GetPosition(father).X / father.ActualWidth;
        Value = Maximum * percent;         
    }         
    
    private void border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)         
    {             
    	hc = !Mouse.Capture(null);          
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.