开发者

Hide ticks at Min and Max in WPF Slider

开发者 https://www.devze.com 2022-12-22 22:25 出处:网络
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 defi开发者_如何学Pythonned a Slider as follows:

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 defi开发者_如何学Pythonned 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}"/>

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.


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.

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.


Please note that you can set the Foreground brush of a Slider to Transparent, which will draw any/all tick marks invisibly. You can then add the center tick using an element/control outside of the Slider (e.g., put both a Slider and a Line shape element in the same grid cell, center aligning the Line element).

Not elegant, but has the advantage of being easily done.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号