如何在 WPF 中更改滑块的选择颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14557965/
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 change slider's selection color in WPF
提问by Hossein Narimani Rad
I have a slider. Is there any way to change the blue color of the selection region to another color (e.g. Black)
我有一个slider. 有没有办法将选择区域的蓝色更改为另一种颜色(例如黑色)


回答by Nick
Overriding system colors won't work with custom templates.
覆盖系统颜色不适用于自定义模板。
Set the "IsSelectionRangeEnabled" to true, "SelectionStart" to your beginning value and "SelectionEnd" to your end value. You can bind "SelectionEnd" to "Value" if you want it to be automatic...
将“IsSelectionRangeEnabled”设置为true,将“SelectionStart”设置为起始值,将“SelectionEnd”设置为最终值。如果您希望它是自动的,您可以将“SelectionEnd”绑定到“Value”...
<Style x:Key="SliderStyle" TargetType="{x:Type Slider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
<Grid VerticalAlignment="Center">
<Border x:Name="borderBackground" Margin="6,0" Height="4" Background="Gray" />
<Canvas Margin="0,-4,0,0" VerticalAlignment="Center">
<Border x:Name="PART_SelectionRange" HorizontalAlignment="Left" Height="4" Background="{TemplateBinding Foreground}" />
</Canvas>
<Track x:Name="PART_Track">
<Track.Thumb>
<Thumb Width="10" Height="20" />
</Track.Thumb>
</Track>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsSelectionRangeEnabled" Value="True" />
<Setter Property="SelectionStart" Value="{Binding Minimum, RelativeSource={RelativeSource Self}}" />
<Setter Property="SelectionEnd" Value="{Binding Value, RelativeSource={RelativeSource Self}}" />
<Setter Property="Foreground" Value="Red" />
</Style>
回答by sa_ddam213
You can override the default SystemColorsto change the color of the selection area.
您可以覆盖默认值SystemColors以更改选择区域的颜色。
<Slider Margin="0,10,0,0" Width="287" Value="6" IsSelectionRangeEnabled="True" SelectionEnd="6" >
<Slider.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlDarkDarkBrushKey}" Color="Silver" />
</Slider.Resources>
</Slider>
Result:
结果:



