Universal Windows Platform – Font Control

Font Control demonstrates how to create a FontSelector 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 as FontControl 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

From the Menu choose Tools, then NuGet Package Manager and Package Manager Console


Step 6

Then in the Package Manager Console Window which usually appears at the bottom of Visual Studio 2017 at the PM> Prompt type in following:

Install-Package Win2D.uwp


Followed by typing Enter to install Win2D.uwp from NuGet into the Application which should be Successfully installed

Step 7

Once done select from the Menu, Project, then Add New Item…


Step 8

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as FontSelector.cs before selecting Add to add the file to the Project


Step 9

Once in the Code View for FontSelector.cs the following should be entered:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;

namespace FontControl
    public class FontSelector : ComboBox
        public FontSelector()
            ItemTemplate = (DataTemplate)XamlReader.Load(
            "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
            "<TextBlock Text=\"{Binding}\" FontFamily=\"{Binding}\"/></DataTemplate>");
            ItemsSource = Microsoft.Graphics.Canvas.Text.CanvasTextFormat.GetSystemFontFamilies();

        public FontFamily Selected
            get { return new FontFamily((string)SelectedItem); }
            set { SelectedValue = value.Source; }

In the FontSelector.cs there are using statements to include the necessary functionality. The FontSelector Constructor is used to initialise the look-and-feel of the Control which is based upon a ComboBox and provides it with a DataTemplate and then sets the ItemsSource of the Control to be the Installed Fonts using the Win2D.uwp Method of GetSystemFontFamilies in the Microsoft.Graphics.Canvas.Text.CanvasTextFormat Namespace which was added with that NuGet Package then there’s a Selected Property to get or set the Font.

Step 10

Once done select from the Menu, Build, then Build Solution


Step 11

In the Solution Explorer select MainPage.xaml


Step 12

From the Menu choose View and then Designer


Step 13

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Grid Margin="50">
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	<local:FontSelector Grid.Row="0" x:Name="Picker" HorizontalAlignment="Stretch" 
	<TextBlock Grid.Row="1" Name="Display" TextWrapping="Wrap" FontSize="20" 
	HorizontalAlignment="Center" VerticalAlignment="Center" Loaded="Display_Loaded"/>

The block of XAML contains a Grid with two rows, the first is for the FontSelector itself and the SelectionChanged Event has been bound to an Event Handler and the second row is for a TextBlock and has it’s Loaded Event bound to an Event Handler.

Step 14

From the Menu choose View and then Code


Step 15

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

private void Selector_SelectionChanged(object sender, SelectionChangedEventArgs e)
	Display.Text = "abcdefghijklmnopqrstuvwxyz " +
	"1234567890.:,; '\"(!?) +-*/=\n\n" +
	"The quick brown fox jumps over the lazy dog";
	Display.FontFamily = Picker.Selected;

private void Display_Loaded(object sender, RoutedEventArgs e)
	Picker.Selected = Display.FontFamily;

Below the MainPage() Method there is a Selector_SelectionChanged Method which will handle when the FontSelector has its Selected Font changed – it will set the TextBlock to a set of Characters then set the FontFamily of the TextBlock to the Selected Value.

Step 16

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application


Step 17

After the Application has started running you can select a Font from the list to see a preview of it in the TextBlock of the Application


Step 18

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