Fun with fonts

So I’ve been playing around with the RichTextBox for WPF and decided that it would be a great idea to add font selection to the code. Obviously, this being WPF, I didn’t want to just list the fonts out, I wanted to list the fonts out in exactly the way they’d be displayed. In other words, I want the font name to be written out using the font itself. By now it shouldn’t come as a surprise to you that this is extremely easy to do in WPF.

First of all, it’s really easy to get a list of the fonts. .NET provides a handy little class cunningly enough known as InstalledFontCollection, so we’ll wrap that up in a handy list ready for use:


using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Drawing;

namespace FontManager
    public class InstalledFonts : List<FontFamily>
        public InstalledFonts()
            InstalledFontCollection fonts = new InstalledFontCollection();

This class just wraps up the installed font families into a handy dataprovider format. This is all about being nice and blend-friendly.

Next we want to define a usercontrol to display the fonts. Something to note about this control; we display the data in a virtualizing stack panel – if you don’t, you could end up waiting quite a while for the first display of the font.

        <Style x:Key="FontStyle">
            <Setter Property="Control.FontFamily" Value="{Binding Name}" />
            <Setter Property="Control.FontSize" Value="16" />
        <DataTemplate x:Key="FontTemplate">
            <StackPanel VirtualizingStackPanel.IsVirtualizing="True">
                    Text="{Binding Name}"
                    ToolTip="{Binding Name}"
                    Style="{StaticResource FontStyle}" />
        <ObjectDataProvider x:Key="FontProvider" ObjectType="{x:Type m:InstalledFonts}"/>
            ItemsSource="{Binding Source={StaticResource FontProvider}}"
            ItemTemplate="{StaticResource FontTemplate}" />


That’s it – that’s all there is to displaying your font names in the appropriate font. It is so easy, and yet another reason to love WPF. Go on – you know you love it.

About these ads
  1. March 14, 2009 at 8:27 am

    nice one pete

    • peteohanlon
      March 14, 2009 at 10:26 am

      Cheers Sacha. I found the one area that I couldn’t find any output from you on – document management in WPF.;->

  2. Richard
    April 22, 2009 at 3:10 pm

    Since WPF doesn’t use System.Drawing, wouldn’t it be “better” to use the System.Windows.Media.Fonts.SystemFontFamilies property instead?

  1. No trackbacks yet.

Leave a Reply

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

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

Google+ photo

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

Connecting to %s

Train of Brain

All aboard.

The Canny Coder

Java 8 Functional Programming with Lambda Expressions

Adventures in theoretical computer science, with your host, chaiguy1337

Confessions of a coder

Confessions of a WPF lover is the best place for your personal blog or business site.


Get every new post delivered to your Inbox.

Join 39 other followers

%d bloggers like this: