Custom Slider demonstrates how to create a custom Style for a Slider Control
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once the Project is created from the Solution Explorer select App.xaml
Step 6
From the Menu choose View and then Designer
Step 7
Once in the Design View for App.xaml between the Application and /Application elements the following should be entered:
<Application.Resources> <Style x:Key="CustomSlider" TargetType="Slider"> <Setter Property="Background" Value="LightSalmon"/> <Setter Property="BorderBrush" Value="Salmon"/> <Setter Property="BorderThickness" Value="2"/> <Setter Property="Foreground" Value="Gold"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Slider"> <Grid Margin="{TemplateBinding Padding}"> <Grid.Resources> <Style x:Key="SliderThumbStyle" TargetType="Thumb"> <Setter Property="BorderThickness" Value="2"/> <Setter Property="BorderBrush" Value="Goldenrod"/> <Setter Property="Foreground" Value="Gold"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Thumb"> <Ellipse StrokeThickness="2" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Foreground}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid x:Name="SliderContainer" Background="Transparent" Grid.Row="1"> <Grid x:Name="HorizontalTemplate"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="17"/> <RowDefinition Height="Auto"/> <RowDefinition Height="17"/> </Grid.RowDefinitions> <Rectangle x:Name="HorizontalTrackRect" Grid.ColumnSpan="3" Fill="{TemplateBinding Background}" Grid.Row="1" Height="10" RadiusX="5" RadiusY="5"/> <Rectangle x:Name="HorizontalDecreaseRect" Fill="{TemplateBinding Background}" Grid.Row="1" Height="10" RadiusX="5" RadiusY="5"/> <Rectangle x:Name="HorizontalBorder" Grid.ColumnSpan="3" Grid.Row="1" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" Height="10" RadiusX="5" RadiusY="5"/> <Thumb x:Name="HorizontalThumb" AutomationProperties.AccessibilityView="Raw" Background="{ThemeResource SliderThumbBackgroundThemeBrush}" Grid.Column="1" DataContext="{TemplateBinding Value}" Grid.Row="1" Style="{StaticResource SliderThumbStyle}" Height="25" Width="25"/> </Grid> <Grid x:Name="VerticalTemplate" Visibility="Collapsed"> <Grid.ColumnDefinitions> <ColumnDefinition Width="17"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="17"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Rectangle x:Name="VerticalTrackRect" Grid.Column="1" Fill="{TemplateBinding Background}" Grid.RowSpan="3" Width="10" RadiusX="5" RadiusY="5"/> <Rectangle x:Name="VerticalDecreaseRect" Grid.Column="1" Fill="{TemplateBinding Background}" Grid.Row="2"/> <Rectangle x:Name="VerticalBorder" Grid.RowSpan="3" Grid.Column="1" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" Width="10" RadiusX="5" RadiusY="5" /> <Thumb x:Name="VerticalThumb" AutomationProperties.AccessibilityView="Raw" Background="{ThemeResource SliderThumbBackgroundThemeBrush}" Grid.Column="1" DataContext="{TemplateBinding Value}" Grid.Row="1" Style="{StaticResource SliderThumbStyle}" Height="25" Width="25"/> </Grid> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Application.Resources>
In the App.xaml there is a CustomSlider resource Style declared and it is a customised copy of the default ControlTemplate for a Slider Control with the Track changed to appear as a rounded-rectangle and Thumb as a circle of the Slider.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Slider Margin="50" VerticalAlignment="Center" HorizontalAlignment="Stretch" Style="{StaticResource CustomSlider}"/>
The block of XAML represents the Slider Control with the Style of the the CustomSlider.
Step 11
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 12
After the Application has started running it should then appear with the Style of the Custom Slider displayed
Step 13
To Exit the Application select the Close button in the top right of the Application