在 WPF 滑块的最小值和最大值处隐藏刻度 [英] Hide ticks at Min and Max in WPF Slider

查看:25
本文介绍了在 WPF 滑块的最小值和最大值处隐藏刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个范围从 0.5 到 1.5 的滑块,在 1.0 处只有一个刻度线来标记中心和默认值.我定义了一个 Slider 如下:

I want to display a Slider ranging from 0.5 to 1.5 with only one tick mark at 1.0 to mark the center and default value. I have defined a Slider as follows:

<Slider Minimum="0.5" Maximum="1.5"
        IsMoveToPointEnabled="True" IsSnapToTickEnabled="False"
        Orientation="Horizontal"
        Ticks="1.0"
        TickPlacement="BottomRight"
        Value="{Binding SomeProperty, Mode=TwoWay}"/>

但是,除了 1.0 处的刻度线外,此滑块还在 0.5 和 1.5 处显示刻度线,即最小值和最大值.

However, besides a tick mark at 1.0 this Slider also shows tick marks at 0.5 and 1.5, i.e. the Minimum and Maximum values.

有没有办法隐藏这些最小/最大刻度线?!我检查了所有属性并尝试更改其中一些属性,但到目前为止没有成功.

Is there a way to hide these min/max tick marks?! I checked all properties and tried changing some of them, but did not have success so far.

推荐答案

一种方法是为 Slider 提供您自己的 ControlTemplate.如果您查看默认的 Slider ControlTemplate(可在 MSDN 上获得),您将看到 Slider 上的 Ticks 是由一个名为 TickBar 的类实现的.
从 TickBar 类继承并覆盖它的 OnRender() 方法将允许您根据需要绘制刻度.您可以在这里获得真正的创意:即编号的刻度、不同的颜色/样式等......或者在您的情况下,根本不要在最小/最大位置绘制刻度.
然后用您的新 TickBar sans min/max ticks 替换自定义 Slider ControlTemplate 中的默认 TickBar,您就可以开始了.

One way to do it would be to provide your own ControlTemplate for the Slider. If you take a look at the default Slider ControlTemplate(available on MSDN), you will see the Ticks on the Slider are implemented by a class called TickBar.
Inherit from the TickBar class and override its OnRender() method will allow you to draw the ticks however you want. You can get really creative here: i.e. numbered ticks, different colors/styles etc... Or in your case, simply don't draw ticks at the min/max position.
Then substitute the default TickBar in the custom Slider ControlTemplate with your new TickBar sans min/max ticks, and you are good to go.

有关从 TickBar 继承的代码示例,请查看以下内容:http://social.msdn.microsoft.com/forums/en-US/wpf/thread/6b926273-b0a1-43ab-be81-f2e9b42d701a

For code examples on inheriting from the TickBar look at this: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/6b926273-b0a1-43ab-be81-f2e9b42d701a

该网站谈到了显示编号的刻度,但您应该能够从中了解您需要了解的内容.

The website talks about displaying numbered ticks but you should be able to learn what you need to know from it.

这篇关于在 WPF 滑块的最小值和最大值处隐藏刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆