This is a migrated thread and some comments may be shown as answers.

MouseOver Tooltip at Slider Track Position

2 Answers 788 Views
Slider
This is a migrated thread and some comments may be shown as answers.
Jose
Top achievements
Rank 1
Jose asked on 08 May 2015, 08:35 PM

Hi,

 Is there a way to show a tooltip with the value of the slider of the current position of the mouse over the track? So the user know the value is going to select beforehand.

Thanks, Jose

2 Answers, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 13 May 2015, 08:30 AM
Hello Jose,

The described functionality is not supported out of the box by RadSlider. However, you can implement it with additional code. Basically, you can subscribe for the MouseMove event and based on several factors - like track width, height, max value, min value and mouse position - calculate a prediction value that will be displayed in the tool tip.
public MainWindow()
{
    InitializeComponent();
    this.slider.Loaded += slider_Loaded;
    this.slider.MouseMove += slider_MouseMove;
}
 
private ContentControl sliderTrack;
 
// Get the ContentControl that represents the track of the slider
void slider_Loaded(object sender, RoutedEventArgs e)
{
    this.sliderTrack = this.slider.ChildrenOfType<ContentControl>().FirstOrDefault(x => x.Name == "Track");
}
 
// Calculate the tooltip value based on the mouse position and the slider's size, and values.
void slider_MouseMove(object sender, MouseEventArgs e)
{           
    Point positionUnderMouse = e.GetPosition(this.sliderTrack);
    var predictedValue = PixelsToValue(positionUnderMouse.X, this.slider.Minimum, this.slider.Maximum, this.sliderTrack.ActualWidth);
// set the tooltip
}
 
private double PixelsToValue(double pixels, double minValue, double maxValue, double width)
{
    var range = maxValue - minValue;
    double percentage = ((double)pixels / width) * 100;
    return ((percentage / 100) * range) + minValue;
}
You can set the tooltip by storing the predictedValue in a viewmodel and bind it to the tooltip of the slider.
public class MainViewModel : ViewModelBase
{
    private double predictedValue;
    public double PredictedValue
    {
        get
        {
            return this.predictedValue;
        }
        set
        {
            if (this.predictedValue != value)
            {
                this.predictedValue = value;
                OnPropertyChanged("PredictedValue");
            }
        }
    }
}

<telerik:RadSlider telerik:RadToolTipService.ToolTipContent="{Binding PredictedValue}" />
For your convenience I prepared a sample project demonstrating this approach. Please give it a try and let me know if is useful.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Jose
Top achievements
Rank 1
answered on 13 May 2015, 06:23 PM

Thanks Martin! Just what I needed.

 Jose

Tags
Slider
Asked by
Jose
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Jose
Top achievements
Rank 1
Share this question
or