Universal Windows Platform – Custom Toggle

Custom Toggle demonstrates how to create a custom Style for a ToggleSwitch 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:

	<Style x:Key="CustomToggle" TargetType="ToggleSwitch">
		<Setter Property="Background" Value="LightSalmon"/>
		<Setter Property="BorderBrush" Value="Salmon"/>
		<Setter Property="Foreground" Value="Gold"/>
		<Setter Property="ManipulationMode" Value="System,TranslateX"/>
		<Setter Property="UseSystemFocusVisuals" Value="True"/>
		<Setter Property="Template">
				<ControlTemplate TargetType="ToggleSwitch">
					<Grid HorizontalAlignment="Center">
							<VisualStateGroup x:Name="ToggleStates">
									<VisualTransition x:Name="DraggingToOnTransition" From="Dragging" GeneratedDuration="0" To="On">
											<RepositionThemeAnimation FromHorizontalOffset="{Binding 
											RelativeSource={RelativeSource Mode=TemplatedParent}}" 
									<VisualTransition x:Name="DraggingToOffTransition" From="Dragging" GeneratedDuration="0" To="Off">
											<RepositionThemeAnimation FromHorizontalOffset="{Binding 
											RelativeSource={RelativeSource Mode=TemplatedParent}}" 
									<VisualTransition x:Name="OnToOffTransition" From="On" GeneratedDuration="0" To="Off">
											<RepositionThemeAnimation FromHorizontalOffset="{Binding 
											RelativeSource={RelativeSource Mode=TemplatedParent}}" 
									<VisualTransition x:Name="OffToOnTransition" From="Off" GeneratedDuration="0" To="On">
											<RepositionThemeAnimation FromHorizontalOffset="{Binding 
											RelativeSource={RelativeSource Mode=TemplatedParent}}" 
								<VisualState x:Name="Dragging"/>
								<VisualState x:Name="Off"/>
								<VisualState x:Name="On">
										<DoubleAnimation Duration="0" To="24" Storyboard.TargetProperty="X"
										<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity"
											<DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
											<DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
											<DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
						<Rectangle x:Name="OuterBorder" Height="30" Width="55" RadiusY="15" RadiusX="15" 
						StrokeThickness="1" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}"/>
						<Rectangle x:Name="SwitchKnobBounds" Height="30" Width="55" RadiusY="15" RadiusX="15"
						StrokeThickness="1" Stroke="Goldenrod" Fill="{TemplateBinding Foreground}" Opacity="0"/>
						<Grid x:Name="SwitchKnob" Grid.Row="2" HorizontalAlignment="Left" Height="25" Width="30">
								<TranslateTransform x:Name="KnobTranslateTransform"/>
							<Ellipse x:Name="SwitchKnobOn" Height="15" Width="15"
							Fill="{TemplateBinding Background}" Opacity="0"/>
							<Ellipse x:Name="SwitchKnobOff" Height="15" Width="15"
							Fill="{TemplateBinding Foreground}"/>
						<Thumb x:Name="SwitchThumb" AutomationProperties.AccessibilityView="Raw">
								<ControlTemplate TargetType="Thumb">
									<Rectangle Fill="Transparent"/>

In the App.xaml there is a CustomToggle resource Style declared and it is a customised copy of the default ControlTemplate for a ToggleSwitch Control with the colours customised like the other Custom Controls.

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:

<ToggleSwitch VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource CustomToggle}"/>

The block of XAML represents the CheckBox Control with the Style of the the CustomCheckbox.

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 started running you can tap the ToggleSwitch to switch between On or Off in the Application


Step 13

To Exit the Application select the Close button in the top right of the Application


Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s