Universal Windows Platform – Custom Rating

Custom Rating demonstrates how to create a custom Style for the RatingControl

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, version 1803 (10.0; Build 17134) which is the April 2018 Update 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>
	<RatingItemFontInfo x:Key="RatingControlFontInfo" Glyph=""/>
	<Style x:Key="CustomRating" TargetType="RatingControl">
		<Setter Property="Foreground" Value="{ThemeResource RatingControlCaptionForeground}"/>
		<Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}"/>
		<Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
		<Setter Property="ItemInfo" Value="{StaticResource RatingControlFontInfo}"/>
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="RatingControl">
					<Grid x:Name="LayoutRoot">
						<VisualStateManager.VisualStateGroups>
							<VisualStateGroup x:Name="CommonStates">
								<VisualState x:Name="Disabled">
									<VisualState.Setters>
										<Setter Target="ForegroundContentPresenter.Foreground" Value="{ThemeResource RatingControlDisabledSelectedForeground}"/>
									</VisualState.Setters>
								</VisualState>
								<VisualState x:Name="Placeholder">
									<VisualState.Setters>
										<Setter Target="ForegroundContentPresenter.Foreground" Value="Green"/>
									</VisualState.Setters>
								</VisualState>
								<VisualState x:Name="PointerOverPlaceholder">
									<VisualState.Setters>
										<Setter Target="ForegroundContentPresenter.Foreground" Value="{ThemeResource RatingControlPointerOverPlaceholderForeground}"/>
									</VisualState.Setters>
								</VisualState>
								<VisualState x:Name="PointerOverUnselected">
									<VisualState.Setters>
										<Setter Target="ForegroundContentPresenter.Foreground" Value="{ThemeResource RatingControlPointerOverUnselectedForeground}"/>
									</VisualState.Setters>
								</VisualState>
								<VisualState x:Name="Set">
									<VisualState.Setters>
										<Setter Target="ForegroundContentPresenter.Foreground" Value="Salmon"/>
									</VisualState.Setters>
								</VisualState>
								<VisualState x:Name="PointerOverSet">
									<VisualState.Setters>
										<Setter Target="ForegroundContentPresenter.Foreground" Value="Gold"/>
									</VisualState.Setters>
								</VisualState>
							</VisualStateGroup>
						</VisualStateManager.VisualStateGroups>
						<StackPanel Margin="-20,-20,-20,-20" Orientation="Horizontal" Grid.Row="0">
							<StackPanel x:Name="RatingBackgroundStackPanel" Background="Transparent" Margin="20,20,0,20" Orientation="Horizontal"/>
							<TextBlock x:Name="Caption" AutomationProperties.AccessibilityView="Raw" Height="32" IsHitTestVisible="False" Margin="4,9,20,0" AutomationProperties.Name="RatingCaption" Style="{ThemeResource CaptionTextBlockStyle}" TextLineBounds="TrimToBaseline" Text="{TemplateBinding Caption}" VerticalAlignment="Center"/>
						</StackPanel>
						<ContentPresenter x:Name="ForegroundContentPresenter" IsHitTestVisible="False" Grid.Row="0">
							<StackPanel Margin="-40,-40,-40,-40" Orientation="Horizontal">
								<StackPanel x:Name="RatingForegroundStackPanel" IsHitTestVisible="False" Margin="40,40,40,40" Orientation="Horizontal"/>
							</StackPanel>
						</ContentPresenter>
					</Grid>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</Application.Resources>

Within the App.xaml is defined an Application.Resources with a Style of “CustomRating” and a TargetType of Rating – this example takes a copy of the style for the standard Rating and customises many of the properties including Glyph set to to the Unicode value of the Heart.

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:

<Viewbox Margin="50">
	<RatingControl Margin="50" Name="Rating" Value="4" Style="{StaticResource CustomRating}"/>
</Viewbox>

The block of XAML represents the RatingControl Control with the Style of the the CustomRating and is within a Viewbox.

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 then select the Rating to display a different number of Heart items selected

ran-custom-ratring

Step 13

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

vs2017-close

For this example Expression Blend was used to create a copy of the standard Style for the RatingControl Content which was then customised with different default colours and glyph used to represent the rating, more sophisticated customisation is possible – the example is just the start of the idea of creating a completely custom look to an application by modifying controls in this way.

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 )

Google+ photo

You are commenting using your Google+ 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