Creating a Stepped Slider Control for Windows Phone 8

In Windows Phone 8, we can add a slider control onto a XAML page using the Slider element.

<Slider Name="MySlider"
    Maximum="12" Minimum="-12" />

Slider Controls

As can be seen, the slider does not step to discrete values (e.g. 0.0, 0.5, 1.0 etc.), but instead allows any value within the Maximum and Minimum range to be set.

For some applications, this would not be expected behaviour, for example using a Slider control to represent hours in a day where only whole hours are allowed.

Fortunately, a simple Slider can easily be given stepping functionality by overriding its ValueChanged event handler.

To do this, first we must add a ValueChanged handler into the XAML definition of the slider.

<Slider Name="MySlider"
    ValueChanged="MySlider_ValueChanged" />

Secondly, we must add a ValueChanged handler into the XAML page’s backing code and set the Value of the slider control to be the correct stepped value.

For example, to make the slider control have discrete steps of 0.5:

private void MySlider_ValueChanged(object sender,
                                   RoutedPropertyChangedEventArgs<double> e)
    MySlider.Value = (Math.Round(e.NewValue/0.5)/2.0);

Slider Controls