Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to restrict a slider to the Selected Range. Ex:

Slider with minimum=0 and maximum=10

SelectionStart=3
SelectionEnd=7

I would like to restrict the slider thumb to only between 3 and 7.

I want the thumb to only be able to select 3 and 7 and any values in between.
Thumb should not be able to move left or right of the range.
Min and Max must stay as 0 and 10.
The Selected Range is dynamic, could change to 5 to 10 or 0 to 6 for example.


thanks in advance.
Posted
Updated 9-Jan-15 10:02am
v2
Comments
Sergey Alexandrovich Kryukov 9-Jan-15 15:40pm    
Not 100% clear. What you are trying to use it for and how? Do you want the slider to stop at 3 or 7 if moved to far left or right? Why not changing ins Maximum and Minimum (asking just in case)?
—SA
Member 11317966 9-Jan-15 16:03pm    
Updated question to clarify

1 solution

Hello,

Add an event handler for ValueChanged in the xaml:

C#
<slider x:name="slider" valuechanged="Slider_ValueChanged" />


Check the slider value in the code behind:

C#
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    if (e.NewValue < 3 | e.NewValue > 7)
        slider.Value = e.OldValue;
}


Valery.
 
Share this answer
 
v2
Comments
Member 11317966 9-Jan-15 16:06pm    
can the 3 an 7 in your solution be dynamic, the Selected Range is variable.
Valery Possoz 9-Jan-15 16:16pm    
yes use a variable for 3 and for 7.

int _min = selectedRange.Min;
int _max = selectedRange.Max;

and use them in the method...

if (e.NewValue < _min | e.NewValue > _max)
Member 11317966 12-Jan-15 12:26pm    
I would like to bind to the slider SelectionStart for the min and SelectionEnd for max.
ex: int _min =slider.SelectionStart;
int _max =slider.SelectionEnd;

but this throws and error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900