2

我需要以下帮助:我希望我的滑块捕捉到 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 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;
    }

滑块在被操纵时效果很好,但是当我更改 SomeProp 时,绑定不起作用。没有事件处理绑定可以正常工作。

4

1 回答 1

3

我在这里找到了一个解决方案(稍微修改了一下):

<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".不知道为什么。

于 2013-01-28T10:17:05.383 回答