Goofy
Goofy

Reputation: 6128

Custom Style to slider

I have a slider, below is the image on how it looks.

enter image description here

I have a white background, because of the white background the button of the slider is not visible at all, so how to change the style of that?

I have searched for it but didn't find proper help.

Upvotes: 1

Views: 1487

Answers (2)

reza.cse08
reza.cse08

Reputation: 6178

You should change the "HorizontalCenterElement" rectangle fill color in slider style.

Here is my source code CustomSliderDesign

<!--change the rectangle-->
<Rectangle x:Name="HorizontalCenterElement"
           Fill="Blue"
           HorizontalAlignment="Left"
           Height="24"
           Margin="0,16,0,44"
           Width="12">
          <Rectangle.RenderTransform>
             <TranslateTransform />
          </Rectangle.RenderTransform>
 </Rectangle>

Upvotes: 0

Romasz
Romasz

Reputation: 29792

I've managed to customize my slider like this (changing defalut template):

In XAML:

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="SliderStyle1" TargetType="Slider">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Maximum" Value="10"/>
        <Setter Property="Minimum" Value="0"/>
        <Setter Property="Value" Value="0"/>
        <Setter Property="Background" Value="{StaticResource PhoneChromeBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Slider">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To="0.1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HorizontalTrack"/>
                                        <DoubleAnimation Duration="0" To="0.1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalTrack"/>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="HorizontalFill">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="VerticalFill">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid x:Name="HorizontalTemplate" Margin="{StaticResource PhoneHorizontalMargin}">
                            <Rectangle x:Name="HorizontalTrack" Fill="{TemplateBinding Background}" Height="12" IsHitTestVisible="False" Margin="0,22,0,50"/>
                            <!--<Rectangle x:Name="HorizontalFill" Fill="{TemplateBinding Foreground}" Height="12" IsHitTestVisible="False" Margin="0,22,0,50">-->
                                <Rectangle x:Name="HorizontalFill" Fill="BlueViolet" Height="12" IsHitTestVisible="False" Margin="0,22,0,50">
                                <Rectangle.Clip>
                                    <RectangleGeometry Rect="0, 0, 6, 12"/>
                                </Rectangle.Clip>
                            </Rectangle>
                            <!--<Rectangle x:Name="HorizontalCenterElement" Fill="{StaticResource PhoneForegroundBrush}" HorizontalAlignment="Left" Height="24" Margin="0,16,0,44" Width="12" Fill="Azure">-->
                                <Rectangle x:Name="HorizontalCenterElement" HorizontalAlignment="Left" Height="48" Margin="0,16,0,44" Width="12" Fill="Coral">
                                <Rectangle.RenderTransform>
                                    <TranslateTransform/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </Grid>
                        <Grid x:Name="VerticalTemplate" Margin="{StaticResource PhoneVerticalMargin}">
                            <Rectangle x:Name="VerticalTrack" Fill="{TemplateBinding Background}" IsHitTestVisible="False" Margin="18,0,18,0" Width="12"/>
                            <Rectangle x:Name="VerticalFill" Fill="{TemplateBinding Foreground}" IsHitTestVisible="False" Margin="18,0,18,0" Width="12">
                                <Rectangle.Clip>
                                    <RectangleGeometry Rect="0, 0, 12, 6"/>
                                </Rectangle.Clip>
                            </Rectangle>
                            <!--<Rectangle x:Name="VerticalCenterElement" Fill="{StaticResource PhoneForegroundBrush}" Height="12" Margin="12,0,12,0" VerticalAlignment="Top" Width="24">-->
                                <Rectangle x:Name="VerticalCenterElement" Fill="Azure" Height="48" Margin="12,0,12,0" VerticalAlignment="Top" Width="24">
                                <Rectangle.RenderTransform>
                                    <TranslateTransform/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

I've changed the Fill of rectangle, the track, and the size of the rectangle. As I've tested it on emulator - works. As you probably know (but for the followers):

<Slider Orientation="Horizontal" HorizontalAlignment="Stretch" Grid.Row="2" Style="{StaticResource SliderStyle1}"/>

Try it, maybe it will help.


If you want to make it Circle (Ellipse) it can look like this:

<Ellipse x:Name="HorizontalCenterElement" HorizontalAlignment="Left" Height="32" Margin="0,16,0,44" Width="12" Fill="Coral">
       <Ellipse.RenderTransform>
         <TranslateTransform/>
       </Ellipse.RenderTransform>
</Ellipse>

You can use also Path to make different figures example here.

Upvotes: 1

Related Questions