In this post I will share how you can set custom fonts for WPF Labels and for other WPF controls using WPF Styles.
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”
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:
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:
- WPF Create Login Form with SQL Database
- WPF – DataGrid with Buttons and its Clicking method – Get Row Data with WPF DataGrid Buttons
- WPF – Using DataGrid Row MouseDoubleClick for getting row Data – Example
- WPF – Designing Material Design Tabs in WPF using Dragablz
- WPF – Bind ComboBox using MS SQL database
- WPF Textbox with Rounded Corners
- WPF Textbox changing Focus Style Background Colors.
- WPF – Bind DataGrid using SQL Database
- WPF Button Style with Rounded Corners and Hover Effects
- WPF Textbox Style – Changing Colors on Focus
- WPF ComboBox SelectionChanged – SelectedItem Method
Great work thank you
Pingback: Download Source Code No 108 - ParallelCodes
Pingback: WPF - Create PDF document using iText7 Library - ParallelCodes