tags:

views:

290

answers:

2

I have a slider with minimum value 0 and maximum value 1.

When I currently slide it, the value gets set to a decimal value between 0 and 1, e.g. 0.2342343.

However, I want the value to only be either 0 or 1 (so that my ViewModel property will register the change only if it is 0 or 1 and not multiple times as the user drags it from 0 to 1).

How can I make the slider value only be 0 or 1? I tried SmallChange, LargeChange and SnapsToDevicePixels, but none of these work.

<Slider Name="TheLanguageIndexSlider"
        DockPanel.Dock="Bottom" 
        Minimum="0" 
        Maximum="1" 
        LargeChange="1"
        SmallChange="1"
        SnapsToDevicePixels="True"
        Width="100" 
        Margin="5" 
        Value="{Binding LanguageIndex}"
        HorizontalAlignment="Left"/>
+2  A: 

Set the IsSnapToTickEnabled to true and assign a value of 1 to the TickFrequency property:

<Slider IsSnapToTickEnabled="True"
        Maximum="1" />
Julien Poulin
+2  A: 

from msdn

<Slider Width="100" Value="50" Orientation="Horizontal" HorizontalAlignment="Left" 
    IsSnapToTickEnabled="True" Maximum="3" TickPlacement="BottomRight" 
    AutoToolTipPlacement="BottomRight" AutoToolTipPrecision="2" 
    Ticks="0, 1.1, 2.5, 3"/>

In your case:

<Slider Name="TheLanguageIndexSlider"
    IsSnapToTickEnabled="True"
    Ticks="0, 1"
    DockPanel.Dock="Bottom" 
    Minimum="0" 
    Maximum="1" 
    LargeChange="1"
    SmallChange="1"
    SnapsToDevicePixels="True"
    Width="100" 
    Margin="5" 
    Value="{Binding LanguageIndex}"
    HorizontalAlignment="Left"/>
Jonathan Fingland