C# 如何更改滑块';WPF中的颜色选择

C# 如何更改滑块';WPF中的颜色选择,c#,wpf,slider,controls,customization,C#,Wpf,Slider,Controls,Customization,我有一个滑块。是否有办法将选择区域的蓝色更改为其他颜色(例如黑色) 您可以覆盖默认的SystemColors来更改选择区域的颜色 <Slider Margin="0,10,0,0" Width="287" Value="6" IsSelectionRangeEnabled="True" SelectionEnd="6" > <Slider.Resources> <SolidColorBrush x:Key="{x:

我有一个
滑块
。是否有办法将选择区域的蓝色更改为其他颜色(例如黑色)


您可以覆盖默认的
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>

结果:


覆盖系统颜色不适用于自定义模板

将“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>


我看不出这如何允许设置自定义颜色。要设置自定义颜色,需要指定自定义模板。请参阅更新的答案(精简以关注颜色)。谢谢Nick。我很感激你回来编辑这篇文章,即使是在3年之后。