#815 – Filling a ListBox with a List of All Fonts

The static Fonts.SystemFontFamilies property contains list of all fonts installed on your system.  Each element in the collection referenced by this property is a FontFamily object.

You can build a list of all fonts by binding a ListBox to this collection.  The example below uses a CollectionViewSource element, bound to SystemFontFamilies, which allows us to sort the items in the collection.  We then bind the ListBox to this collection.

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        x:Class="WpfApplication2.MainWindow"
        Width="300" Height="200"
        Title="Display a List of Fonts">

    <Window.Resources>
        <CollectionViewSource x:Key="allFonts"
                              Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Source"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource allFonts}}"
                 Margin="5" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"
                               FontFamily="{Binding}"
                               FontSize="14"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

We display each item in the list using the appropriate font by using a data template and setting the FontFamily property of a TextBlock.

815-001

Advertisements

#814 – You Can Specify a List of Fonts to Try

You normally indicate the font or typeface to use in rendering a control by specifying a value for the FontFamily property.

        <TextBlock Padding="20,10" FontSize="16"
                   FontFamily="Corbel">
            I drank what?  --Socrates
        </TextBlock>

814-001
However, you can also supply a list of fonts for the FontFamily property.  WPF will attempt to use the first font listed, but if the font is not installed on the target system, it will fall back to the next font in the list.  This fallback mechanism will continue, as WPF tries each font in the list.  If none of the fonts listed are found, WPF will use the default font, Segoe UI.

        <TextBlock Padding="20,10" FontSize="16"
                   FontFamily="Baskerville,Georgia">
            I drank what?  --Socrates
        </TextBlock>

814-002

#337 – Specifying Font Properties for All Controls In a Window

Because the various font-related properties are dependency properties, they can be set on a high-level element in the logical tree and “trickle down” to lower-level elements.  The lower-level elements inherit property values from higher-level elements.

In the example below, we specify a font to use for all child controls within a window.

<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
	x:Class="WpfApplication11.MainWindow"
	x:Name="Window"
	Title="Quotes"
	Width="780" Height="368"
    FontFamily="Georgia">

    <StackPanel>
        <Label Content="A nice quote:" Margin="20,10,20,0"/>

        <TextBlock Name="txt2" Margin="20" FontSize="18" TextWrapping="Wrap">
            Freedom is never dear at any price. It is the breath of life. What would a man not pay for living?<LineBreak/>
            --Mohandas Gandhi
        </TextBlock>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <RadioButton Content="Like" Margin="10"/>
            <RadioButton Content="Don't Like" Margin="10"/>
        </StackPanel>

        <Button Content="OK" FontFamily="Tahoma" Width="100" Margin="20"/>
    </StackPanel>


Notice that we can override an inherited property by setting it explicitly for a control. We specify that Tahoma should be the FontFamily for the Button control.

#336 – Intellisense Shows Fonts Available on Development System

When you use the XAML editor in Visual Studio 2010 to enter a value for the FontFamily property, Intellisense in the editor will show you a list of fonts available on the development system.  In the example below, the dropdown shows the Parsons font as a possible font choice, since it has been installed on the development system.

This font will appear correctly when the application is run on the development system, but will get displayed on a target system only if it is also installed on that system.  If the font is not present, some other font will be substituted.

#335 – How WPF Finds Fonts on a Target System

A WPF application specifies a font that it wants using the properties FontFamily, FontStyle, FontWeight, FontStretch and FontSize.  At runtime, WPF decides the exact font to use on the target system where the application is running. This decision is based on matching the first four properties (ignoring FontSize for the moment) to a physical font file that is installed on that system.

WPF starts by matching the supplied FontFamily against the names of the fonts found on the system.  It then tries to find a font that most closely matches the requested FontStretch, FontStyle and FontWeight property values.  Matching FontStretch is the highest priority, followed by FontStyle and then FontWeight.

If WPF can’t find a matching font, it “falls back” to a default font installed with WPF, C:\Windows\Fonts\GlobalUserInterface.CompositeFont.  This is a composite font that tries to map individual characters to fonts likely to be present on the system.

#334 – Specifying Values for FontFamily

The FontFamily property of a Control indicates what typeface the control’s text should be rendered in.  The value of the property can be the name of any of the fonts currently installed on the system where a WPF application is running.  In Windows 7, there are a number of fonts installed by default that can be assumed to always be present.

        <TextBlock Margin="20,20,20,0" FontSize="18" FontFamily="Candara" TextWrapping="Wrap">
            Freedom is never voluntarily given by the oppressor; it must be demanded by the oppressed.<LineBreak/>
            --Martin Luther King, Jr.
        </TextBlock>

        <TextBlock Margin="20,40,20,0" FontSize="18" FontFamily="Constantia" TextWrapping="Wrap">
            Freedom is never dear at any price. It is the breath of life. What would a man not pay for living?<LineBreak/>
            --Mohandas Gandhi
        </TextBlock>

#268 – Default FontFamily and FontSize

In WPF, the default font family for text displayed on controls (like Label and Button) is Segoe UI, with a default size of 12.0 device-independent units.

Because a device-independent unit is 1/96 inch, a FontSize value of 12 represents characters whose capitals are 1/8″ high.  This is also equivalent to a size of 9 points.

Windows also uses 9 point Segoe UI as its default system font (in Windows 7).

Most books use a font that is 11 or 12 pts (14-16 WPF units).  Paperbacks often have slightly smaller type, e.g. 10 pt (13-1/3 WPF Units).

Web sites will also typically use fonts for body text that results in text that ranges from 10-12 points.

Microsoft Word uses 11 pt as its default font size.