开发者

How can I make a Slider control in WPF that has a single snap point?

For example the 'Zoom' control in Microsoft Word/PowerPoint 2010 has a snap point at value 100%.

I know that there is a possibility to make it snap to specific intervals by setting up ticks, and enabling IsSnapToTickEn开发者_JS百科abled, but this is not the case here, where there is a single snap point, and the slider can go free for other values.


You could try a ValueChanged handler.

private void Slider_ValueChanged(
    object sender,
    RoutedPropertyChangedEventArgs<double> e)
{
    var slider = sender as Slider;
    var tick = slider.Ticks
        .Where(xx => Math.Abs(e.NewValue - xx) < slider.LargeChange);
    if (tick.Any())
    {
        var newValue = tick.First();
        if (e.NewValue != newValue)
        {
            DispatcherInvoke(() => slider.Value = newValue);
        }
    }
}

The example Slider had the following settings:

<Slider Ticks="100.0"
        Minimum="0.0"
        Maximum="500.0"
        Value="75.0"
        SmallChange="1.0"
        LargeChange="10.0"
        ValueChanged="Slider_ValueChanged" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜