views:

6968

answers:

3

The title says it all. All too often I want a WPF slider that behaves like the System.Windows.Forms.TrackBar of old. That is, I want a slider that goes from X to Y but only allows the user to move it in discrete integer positions.

How does one do this in WPF since the Value property on the Slider is double?

+14  A: 

The simple answer is that you take advantage of the IsSnapToTickEnabled and TickFrequency properties. That is, turn snapping to ticks on and set the tick frequency to 1.

Or, in other words ... take advantage of ticks ... but you don't necessarily have to show the ticks that you are snapping to.

Check out the following piece of xaml:

<Slider
    Orientation="Vertical"
    Height="200"
    Minimum="0"
    Maximum="10"
    Value="0"
    IsSnapToTickEnabled="True"
    TickFrequency="1"
/>

I just had to pose and answer this question as I just had to figure out for the second time how to do this.

Now it is just a Google or StackOverflow search away ... at least I hope.

cplotts
+9  A: 

If you set your tick marks in the right way, you can use IsSnapToTickEnabled. This worked pretty well for me. See MSDN for details.

Brian Stewart
A: 

Great, it is working. It can be more modified as

<Slider  Name="sldVertical" Minimum="1" Maximum="50" Value="1" 
         Orientation="Vertical" 
        Height="250" Width="22" VerticalAlignment="Top" HorizontalAlignment="Right"
        AutoToolTipPlacement="TopLeft">
</Slider>
amit kumar thakur