WPF滑块 - 捕捉到0.5

时间:2013-01-28 07:55:31

标签: wpf binding slider

我需要以下帮助:我希望我的滑块捕捉到0.5,但我不能使用TickFrequency属性,因为我使用Ticks DoubleCollection,因为非线性缩放。实际上我正在使用刻度来绘制带有如下数字的自定义刻度线:

public class NumberedTickBar : TickBar
{
    protected override void OnRender(DrawingContext dc)
    {
        string text;
        FormattedText formattedText;
        int i;
        double step;
        double minStep = this.ActualHeight / (this.Maximum - this.Minimum);
        double currentPosition = 0;
        // Draw each tick text
        for (i = 0; i < this.Ticks.Count; i++)
        {
            if (i == 0) step = i;
            else step = this.Ticks[i] - this.Ticks[i - 1];

            currentPosition += step * minStep;
            text = "-" + this.Ticks[i].ToString();

            formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Tahoma"), 10, Brushes.DimGray);
            dc.DrawText(formattedText, new Point(22, base.ActualHeight - currentPosition));
        }
    }
}

我的Slider定义如下:

<Slider Name="SomeSlider" Orientation="Vertical" Style="{DynamicResource MySliderStyle}" 
    Focusable="False" Value="{Binding Path=SomeProp, Mode=OneWay}"
    Maximum="35" TickPlacement="TopLeft" Minimum="8" AutoToolTipPlacement="TopLeft"               AutoToolTipPrecision="1" 
    MinHeight="80" Ticks="8,15,20,25,30,35" 
    ValueChanged="SomeSlider_ValueChanged"/>

正如你可以使用的那样,滑块值绑定到依赖属性,当我尝试做一些像:

private void SLAlowCooling_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

    {
        (sender as Slider).Value = Math.Round(e.NewValue * 2, 0) / 2;
    }

Slider在被操作时效果很好但是当我改变SomeProp时,绑定不起作用。没有事件处理绑定工作正常。

1 个答案:

答案 0 :(得分:2)

我找到了solution here(略有修改):

<Slider Name="SomeSlider" Orientation="Vertical" 
        AutoToolTipPlacement="TopLeft" AutoToolTipPrecision="1" 
        Value="{Binding SomeProp}" 
        TickPlacement="TopLeft" Ticks="8,15,20,25,30,35" 
        Minimum="8" Maximum="35" SmallChange="0.5">
    <i:Interaction.Behaviors>
        <wpfGridView:SnappingSlider/>
    </i:Interaction.Behaviors>
</Slider>
public class SnappingSlider : Behavior<Slider>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.ValueChanged += AssociatedObjectValueChanged;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.ValueChanged -= AssociatedObjectValueChanged;
    }

    private void AssociatedObjectValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
    {
        double steps = Math.Round((e.NewValue - AssociatedObject.Minimum) / AssociatedObject.SmallChange);
        if(steps<0)
            return;
        double newValue = AssociatedObject.Minimum + steps*AssociatedObject.SmallChange;
        if(newValue>AssociatedObject.Maximum)
            return;
        AssociatedObject.Value = newValue;
    }
}
  • 您需要对System.Windows.Interactivity的引用 工作的行为。 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

  • 这使滑块捕捉到SmallChange偏移的倍数与最小值

    由于某种原因,它在设计时显示错误The name "SnappingSlider" does not exist in the namespace "clr-namespace:WpfGridView".不知道为什么tbh。