Use Custom Fonts in WPF C# applications

Use Custom Fonts in WPF C# applications 02

In this post I will share how you can set custom fonts for WPF Labels and for other WPF controls using WPF Styles.

Download Source code.

Custom fonts can be specified in WPF C# windows applications for different controls by defining custom fonts in styles. These custom fonts can be True Type fonts (.ttf) or OpenType fonts (.otf). We will see both here.

In your WPF Project folder, add new folder named fonts and add any two fonts you wish. I’m using Moon.otf and Bradley Hand ITC (bradhitc.ttf) fonts. When you specify font name, it is important to specify the name displayed when the font is opened for installation.

Example:

My font name is bradhitc.ttf, but this name won’t work in WPF design styles. The correct name would be “Bradley Hand ITC”

Use Custom Fonts in WPF C# applications 01

 

Let’s see example: WPF Custom Fonts styling example.

MainWindow.xaml:

<Window x:Class="WfpCustomFonts.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="lblDesign" TargetType="Label">
<Setter Property="FontFamily" Value="fonts/moon.otf #moon"/>
</Style>

<Style TargetType="Label">
<Setter Property="FontSize" Value="25"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="DarkGray"/>
<Setter Property="FontFamily" Value="fonts/bradhitc.ttf #Bradley Hand ITC"/>
</Style>
</Window.Resources>

<StackPanel Orientation="Vertical">
<StackPanel Margin="5">
<Label Content="Hello WPF" x:Name="lblHello" Margin="5" Style="{StaticResource lblDesign}"/>

<Label Content="Hello .NET" x:Name="lblNet" Margin="5"/>

<Label Content="Hello YouTube" x:Name="lblYouTube" Margin="5" Foreground="Red"/>
</StackPanel>
</StackPanel>
</Window>

Output:

Use Custom Fonts in WPF C# applications 02

This can be also specified in the app.xaml file in your WPF C# form application like below:

App.xaml:

<Application x:Class="WfpCustomFonts.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="lblDesign" TargetType="Label">
<Setter Property="FontFamily" Value="fonts/moon.otf #moon"/>
</Style>

<Style TargetType="Label">
<Setter Property="FontSize" Value="35"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="DarkGray"/>
<Setter Property="FontFamily" Value="fonts/bradhitc.ttf #Bradley Hand ITC"/>
</Style>
</Application.Resources>
</Application>

 

Download Source Code.
Thank You.
See Also:


3 thoughts on “Use Custom Fonts in WPF C# applications”

  1. Pingback: Download Source Code No 108 - ParallelCodes

  2. Pingback: WPF - Create PDF document using iText7 Library - ParallelCodes

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.