wpf 如何始终在拇指上显示滑块的当前位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28635824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show current position of a slider over the thumb always
提问by newbie14
How to show a slider control with a tooltip/label under the thumb showing the value (timespan) always while it moves with/without user dragging.
如何在拇指下显示带有工具提示/标签的滑块控件,在用户拖动/不拖动时始终显示值(时间跨度)。
I tried AutoToolTipPlacement="BottomRight" AutoToolTipPrecision="3" on my slider. But Tooltip gets displayed only when i drag the thumb. I want that shown even when i invoke playing slider with a button control.(like video player)
我在滑块上尝试了 AutoToolTipPlacement="BottomRight" AutoToolTipPrecision="3"。但是只有当我拖动拇指时才会显示工具提示。即使我使用按钮控件调用播放滑块,我也希望显示它。(如视频播放器)
The point is to reduce the size of my usercontrol and avoid extra labels for timers or position.
关键是减少我的用户控件的大小并避免为计时器或位置添加额外的标签。
If i am in the wrong direction, please suggest me better ideas. Thanks!
如果我的方向错误,请建议我更好的想法。谢谢!
回答by Stewbob
You can re-style the Thumbto show this effect. Below is a sample that makes a circular Thumbwith the .Valueproperty of the parent Slidershowing up inside the circle.
您可以重新设置样式Thumb以显示此效果。下面是一个示例,它制作了一个圆形,其中父级Thumb的.Value属性Slider显示在圆形内。
<Style TargetType="{x:Type Thumb}">
<Setter Property="Focusable" Value="false"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Height" Value="20"/>
<Setter Property="Width" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Canvas SnapsToDevicePixels="true">
<Grid Height="20" Width="20">
<Ellipse x:Name="Background"
Fill="#FFA3A3A3"
Height="20" Width="20"
Stroke="#FFDADADA"/>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="Black"
FontSize="9"
Text="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type Slider}}, Converter={StaticResource ConvertToIntegerConverter}}"/>
</Grid>
</Canvas>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Fill" TargetName="Background"
Value="#FFDADADA"/>
</Trigger>
<Trigger Property="IsDragging" Value="true">
<Setter Property="Fill" TargetName="Background"
Value="#FFF2F2F2"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Fill" TargetName="Background"
Value="#FFF2F2F2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I've used an IValueConverterto make sure the the value displayed is always an integer, since the normal .Valueproperty is a decimal. You would want to use your own converter to properly format the information that you want displayed.
我使用 anIValueConverter来确保显示的值始终是整数,因为正常.Value属性是小数。您可能希望使用自己的转换器来正确格式化要显示的信息。


You can make the text or numbers appear wherever you want by re-styling the Thumb.
您可以通过重新设计Thumb.
EDIT:
编辑:
If you want to show the text above or below the actual thumb, it's a pretty minor change to the styling:
如果您想在实际拇指上方或下方显示文本,则对样式进行很小的更改:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Ellipse x:Name="Background"
Fill="#FFA3A3A3"
Height="20" Width="20"
Stroke="#FFDADADA"/>
<TextBlock Grid.Row="1" HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="White"
FontSize="9"
Text="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type Slider}}, Converter={StaticResource ConvertToIntegerConverter}}"/>
</Grid>



