我需要以下帮助:我希望我的滑块捕捉到 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 时,绑定不起作用。没有事件处理绑定可以正常工作。