Universal Windows Platform – Custom Checkbox

Custom Checkbox demonstrates how to create a custom Style for a CheckBox 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.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-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
vs2017-new-project-window

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.

vs2017-target-platform

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

vs2017-app

Step 6

From the Menu choose View and then Designer

vs2017-view-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="CustomCheckbox" TargetType="CheckBox">
		<Setter Property="Background" Value="Transparent"/>
		<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
		<Setter Property="Padding" Value="5,5,0,0"/>
		<Setter Property="HorizontalAlignment" Value="Left"/>
		<Setter Property="VerticalAlignment" Value="Center"/>
		<Setter Property="HorizontalContentAlignment" Value="Left"/>
		<Setter Property="VerticalContentAlignment" Value="Top"/>
		<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
		<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
		<Setter Property="UseSystemFocusVisuals" Value="True"/>
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="CheckBox">
					<Grid BorderBrush="{TemplateBinding BorderBrush}"
					  BorderThickness="{TemplateBinding BorderThickness}"
					  Background="{TemplateBinding Background}">
						<Grid.ColumnDefinitions>
							<ColumnDefinition Width="30"/>
							<ColumnDefinition Width="*"/>
						</Grid.ColumnDefinitions>
						<VisualStateManager.VisualStateGroups>
							<VisualStateGroup x:Name="CombinedStates">
								<VisualState x:Name="UncheckedNormal"/>
								<VisualState x:Name="CheckedNormal">
									<Storyboard>
										<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity"
										Storyboard.TargetName="InnerRectangle">
											<DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
										</ObjectAnimationUsingKeyFrames>
									</Storyboard>
								</VisualState>
								<VisualState x:Name="CheckedPressed">
									<Storyboard>
										<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity"
										Storyboard.TargetName="InnerRectangle">
											<DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
										</ObjectAnimationUsingKeyFrames>
									</Storyboard>
								</VisualState>
							</VisualStateGroup>
						</VisualStateManager.VisualStateGroups>
						<Grid>
							<Rectangle x:Name="NormalRectangle" Height="30" Width="30" RadiusY="10" RadiusX="10"  
							UseLayoutRounding="False" StrokeThickness="2" Stroke="Salmon" Fill="LightSalmon"/>
							<Rectangle x:Name="InnerRectangle" Height="20" Width="20" RadiusY="4" RadiusX="4"  
							UseLayoutRounding="False" StrokeThickness="2" Stroke="Goldenrod" Fill="Gold" Opacity="0"/>
						</Grid>
						<ContentPresenter x:Name="ContentPresenter" Grid.Column="1"
						AutomationProperties.AccessibilityView="Raw"
						ContentTemplate="{TemplateBinding ContentTemplate}"
						ContentTransitions="{TemplateBinding ContentTransitions}"
						Content="{TemplateBinding Content}"
						HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
						Margin="{TemplateBinding Padding}" TextWrapping="Wrap"
						VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
					</Grid>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</Application.Resources>

In the App.xaml there is a CustomCheckbox resource Style declared and it is a customised copy of the default ControlTemplate for a CheckBox Control with the Tick changed to appear as a rounded-rectangle within another rounded-rectangle.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 9

From the Menu choose View and then Designer

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

<CheckBox Content="Checkbox" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource CustomCheckbox}"/>

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

vs2017-local-machine

Step 12

After the Application has started running you can tap the CheckBox to set it to Checked in the Application

ran-custom-checkbox

Step 13

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

vs2017-close

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