Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change slider's selection color in WPF

I have a slider. Is there any way to change the blue color of the selection region to another color (e.g. Black)

enter image description here

like image 854
Hossein Narimani Rad Avatar asked Jan 28 '13 08:01

Hossein Narimani Rad


2 Answers

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...

<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>
like image 143
Nick Avatar answered Oct 19 '22 21:10

Nick


You can override the default SystemColors to change the color of the selection area.

    <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:

enter image description here

like image 41
sa_ddam213 Avatar answered Oct 19 '22 21:10

sa_ddam213