#1,019 – Constraining a Slider to Integer Values

The Value property of a Slider can take on any value between the Minimum and Maximum values.  Because it is of type double, it will normally take on a series of real number values as the user drags the thumb.

1019-001

If you want to constrain the Slider values to be integer values only, you can set the following properties:

  • TickFrequency : 1
  • IsSnapToTickEnabled : True

Tickmarks will not be shown, but the Slider will now be constrained only to integer values.

        <Slider Name="mySlider" Margin="10"
                Minimum="1" Maximum="100"
                TickFrequency="1" IsSnapToTickEnabled="True"/>
        <StackPanel Orientation="Horizontal">
            <Label Content="Value:" VerticalAlignment="Center"/>
            <TextBlock Text="{Binding Path=Value, ElementName=mySlider}"
                       Margin="10"/>
        </StackPanel>

1019-002