This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Base Classes - Control - Fonts

The twenty-ninth part of the Windows Presentation Foundation Fundamentals tutorial continues to look at the Control class. This instalment examines the properties that may be applied to a control to modify its font, and that of its children.

Font Style

You can add an italic or slanted effect to text using the FontStyle property. In a similar pattern to FontWeight, the value is set using an object of the FontStyle type with the available types defined in static properties of the FontStyles class. When setting the style in XAML you provide an attribute containing one option from Normal, Italic or Oblique.

The default option of "Normal" means that the standard font will be used. If you change the value to Italic, and the selected font family includes an italicised version, the text will be shown in italics. Oblique is subtly different; it skews the standard font to achieve a slanted effect.

To show the differences, add the following horizontal StackPanel after the four labels and within the existing StackPanel.

<StackPanel Orientation="Horizontal">
    <Label FontStyle="Normal">Normal</Label>
    <Label FontStyle="Italic">Italic</Label>
    <Label FontStyle="Oblique">Oblique</Label>

Running the program yields results similar to those shown below. If you look closely you should be able to see the difference between the Italic and Oblique items.

WPF FontStyle Example

Font Size

The size of a font can be modified using the FontSize property. You can set this to a double-precision floating-point number to set a size in device independent units, or you can add a unit to your XAML to use inches, centimetres or points. The measurement units are described in detail in the FrameworkElement article's "Sizing Units" section.

To demonstrate, add another label to the outer StackPanel, as follows:

<Label FontSize="0.2cm">Small Font</Label>

You can see that the text is somewhat smaller that the default value:

WPF FontSize Example

Font Stretch

The final font property supplied by the Control class is FontStretch. This allows you to expand or contract the typeface horizontally, affecting the aspect ratio of the characters. Following a similar pattern to the weight and style values, the property is set to a FontStretch object, with the available options defined in the static properties of the FontStretches type. When using XAML you simply provide the correct name.

The table below shows the available names and the stretching multiplier. Values above 100% indicate that the font is wider than normal. Values below 100% are condensed so appear thinner.

Normal, Medium100%

To complete the example, add a final label:

<Label FontStretch="Condensed">Condensed Font</Label>

Running the program should generate a window similar to that shown below. Note that the final label is narrower than the standard font.

WPF FontStretch Example

2 October 2013