Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / WPF

Fun with Fonts

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
16 Apr 2009CPOL1 min read 29.7K   13   5
How to have fun with fonts in WPF

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:

C#
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.AddRange(fonts.Families);
        }
    }
}

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.

XML
<UserControl
    x:Class="FontManager.InstalledFontDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:drawing="clr-namespace:System.Drawing;assembly=System.Drawing"
    xmlns:m="clr-namespace:FontManager"
    xmlns:sys="clr-namespace:System.Collections.Generic;assembly=mscorlib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <Style x:Key="FontStyle">
            <Setter Property="Control.FontFamily" Value="{Binding Name}" />
            <Setter Property="Control.FontSize" Value="16" />
        </Style>
        <DataTemplate x:Key="FontTemplate">
            <StackPanel VirtualizingStackPanel.IsVirtualizing="True">
                <TextBlock
                    Text="{Binding Name}"
                    ToolTip="{Binding Name}"
                    Style="{StaticResource FontStyle}" />
            </StackPanel>
        </DataTemplate>
        <ObjectDataProvider x:Key="FontProvider" ObjectType="{x:Type m:InstalledFonts}"/>
    </UserControl.Resources>
    <ComboBox
            VerticalAlignment="Top"
            ItemsSource="{Binding Source={StaticResource FontProvider}}"
            ItemTemplate="{StaticResource FontTemplate}" />

</UserControl>

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.

This article was originally posted at http://peteohanlon.wordpress.com?p=226

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Barry Lapthorn6-Feb-12 0:13
protectorBarry Lapthorn6-Feb-12 0:13 
Answera much *MUCH* easier approach Pin
Indrora28-Jun-09 9:56
Indrora28-Jun-09 9:56 
There's an easier way to do it:
<ListBox ItemsSource="{Binding Source={x:Static Member=Fonts.SystemFontFamilies}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Label FontFamily="{Binding .}" Content="{Binding Source}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


It works, dont worry.

----
Morgan Gangwere
Lead programmer, Unknown Software

"Pinky, are you thinking what im thinking?"
"I Dunno brain, how many licks DOES it take to get to the tootsie roll center of a tootsie pop?"
"You want me to calculate that? or should we take over the world?"
"ooh! OooooOOOooH! lets find out!"

GeneralRe: a much *MUCH* easier approach Pin
Pete O'Hanlon28-Jun-09 10:15
subeditorPete O'Hanlon28-Jun-09 10:15 
QuestionFont Sizes Pin
Joel Palmer21-Apr-09 3:56
Joel Palmer21-Apr-09 3:56 
AnswerRe: Font Sizes Pin
Pete O'Hanlon21-Apr-09 4:19
subeditorPete O'Hanlon21-Apr-09 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.