How can I create a slider control in WPF with a single anchor point?

For example, the Zoom control in Microsoft Word / PowerPoint 2010 has an anchor point of 100%.

I know that it is possible to bind it to certain intervals by setting ticks and turning on IsSnapToTickEnabled, but here it is not where there is one anchor point, and the slider may become free for other values.

+3
source share
1 answer

You can try the handler ValueChanged.

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 Sliderhad 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
source

Source: https://habr.com/ru/post/1786697/


All Articles