Click here to Skip to main content
Click here to Skip to main content

A XAML-Only Font ComboBox

, 9 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A XAML-only font combobox.



I needed a simple ComboBox to select a FontFamily in a WPF application (I don't care about the font-weight). After some searching, I found Pete O'Hanlon's article describing what I wanted.

So why another (short!) article? The first commenter in the article suggested this:

<ListBox ItemsSource="{Binding Source={x:Static Member=Fonts.SystemFontFamilies}}">
            <Label FontFamily="{Binding .}" Content="{Binding Source}" />

as an alternative, which made me think about combining the two as a pure XAML solution that you can cut and paste (as Pete's code has a tiny bit of code-behind). In order to create the XAML solution, I found out a few interesting things that I thought I would share in a real example (as I'm still getting to grips with the many facets of WPF).

Show Me the XAML!

Here it is, in its entirety:

          ItemTemplate="{DynamicResource FontTemplate}">

        <CollectionViewSource x:Key="myFonts" Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
                <ComponentModel:SortDescription PropertyName="Source" />

        <Style x:Key="FontStyle">
            <Setter Property="Control.FontFamily" Value="{Binding Source}" />
            <Setter Property="Control.FontSize" Value="16" />

        <DataTemplate x:Key="FontTemplate">
            <StackPanel VirtualizingStackPanel.IsVirtualizing="True">
                <TextBlock Style="{StaticResource FontStyle}"
                           Text="{Binding Source}"
                           ToolTip="{Binding Source}" />


        <Binding Source="{StaticResource myFonts}" />

You should be able to cut 'n' paste this directly into your code. You would then bind the ComboBox's SelectedValue to a property of your choice. The SelectedValue is of type System.Windows.Media.FontFamily.

What is Going On?

There are several things going on that we need to describe. Beware! More verbose XAML!

A Sorted List of Fonts

Skipping directly to the ComboBox.Resources section: we get the full collection of system fonts. However, by default, they only come partially sorted (by FamilyName), so we sort them into our own collection called myFonts. We do this by importing the ComponentModel namespace via this XAML markup:


and then create our own collections sorted by the Source property (which is the font family name):

<CollectionViewSource x:Key="myFonts" Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
        <ComponentModel:SortDescription PropertyName="Source" />

Data Template

We declare a simple template that renders the fonts in their own type face, and provides a tooltip, within the ComboBox.

Static Resources

Lastly we bind the ComboBox.ItemsSource to our sorted collection of fonts, myFonts, using the long-hand XAML binding. Why do we do this last, and not directly as a ComboBox attribute?

The ItemsSource attribute requires that it is bound to a static resource. Suppose we do this:

  ItemTemplate="{DynamicResource FontTemplate}"
  ItemsSource="{Binding Source={StaticResource myFonts}}">

We get an exception thrown:

"Cannot find resource named 'myFonts'. Resource names are case sensitive."

as myFonts has not yet been declared.

We could of course move our font collection to the UserControl/Window/Application Resources section, however in this example we only have one font combo box, so it is nice to have it within the ComboBox.Resources section.

You might also try setting the ItemsSource to reference myFonts dynamically, via:

  ItemTemplate="{DynamicResource FontTemplate}"
  ItemsSource="{Binding Source={DynamicResource myFonts}}">

This also fails with the exception:

"A 'DynamicResourceExtension' cannot be set on the 'Source' property of type 'Binding'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject."

So in answer to our question: as XAML has a 'one-pass compiler', a StaticResource has to be declared lexically before it is referenced: if we declare the binding last, then we can create our sorted list of fonts StaticResource, within ComboBox.Resources, and then bind to it within the XAML of the ComboBox, hence this piece of XAML:

    <Binding Source="{StaticResource myFonts}" />

Using this XAML Snippet

As mentioned above, if you intend on using this XAML (and using the font combobox multiple times), move the sorted font collection:

<CollectionViewSource x:Key="myFonts" Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">

into your Application/Window/UserControl Resources section, and put this attribute:


into the corresponding XAML document root.

Tip o' the hat to Pete for the original XAML.

A Final Word on Safe Font Usage

Never, ever, choose Comic Sans. Ever.


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


About the Author

Barry Lapthorn

United Kingdom United Kingdom
Jack of all trades.

Comments and Discussions

QuestionCould be really good PinmemberReiner Block30-May-14 23:33 
GeneralMy vote of 5 PinmemberPeter Huber SG4-Sep-13 21:52 
GeneralMy vote of 5 PinmemberN_cooL10-Aug-13 4:05 
QuestionWon't work... PinmemberSteveXMon5-Sep-12 8:13 
AnswerRe: Won't work... PinprotectorBarry Lapthorn15-Sep-12 1:21 
GeneralMy vote of 5 PinmemberShi Yingjie13-Mar-12 15:26 
QuestionMessage Automatically Removed Pingroupkjlfdgehe9-Mar-12 16:29 
GeneralMy vote of 5 PinmemberCarstenV213-Feb-12 9:29 
GeneralRe: My vote of 5 PinprotectorBarry Lapthorn23-Feb-12 11:44 
GeneralMy vote of 5 PinprotectorPete O'Hanlon7-Feb-12 4:10 
GeneralRe: My vote of 5 PinprotectorBarry Lapthorn7-Feb-12 4:56 
GeneralRe: My vote of 5 PinprotectorPete O'Hanlon7-Feb-12 6:48 
GeneralGood stuff! PinmvpNish Sivakumar6-Feb-12 6:23 
GeneralRe: Good stuff! PinprotectorBarry Lapthorn6-Feb-12 6:25 
GeneralRe: Good stuff! PinmvpNish Sivakumar6-Feb-12 6:28 
GeneralRe: Good stuff! PinprotectorBarry Lapthorn6-Feb-12 6:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 9 Mar 2012
Article Copyright 2012 by Barry Lapthorn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid