Home > Net >  How is it possible to add a custom font to my WindowsTemplate Studio (WPF) project?
How is it possible to add a custom font to my WindowsTemplate Studio (WPF) project?

Time:05-09

How is it possible to add a custom font to my WindowsTemplate Studio (WPF) project?

The font is placed in a own folder and that's my App.xaml code:

<Application
    x:Class="DicomCameraConfigurationCreator.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="OnStartup"
    Exit="OnExit"
    DispatcherUnhandledException="OnDispatcherUnhandledException">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
                <ResourceDictionary Source="/Styles/_Thickness.xaml" />
                <ResourceDictionary Source="/Styles/MetroWindow.xaml" />
                <ResourceDictionary Source="/Styles/TextBlock.xaml" />
                <!--
                MahApps.Metro resource dictionaries.
                Learn more about using MahApps.Metro at https://mahapps.com/
                -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <!-- Accent and AppTheme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Thanks!

CodePudding user response:

I have already found out how it works for me:

  1. Add your font to a "Fonts" folder

  2. yourFont.ttf (right click) -> Properties:

  • Build Action -> Resource
  • Copy to Output Directory -> Do not copy
  1. Use font in code:
  • xaml -> FontFamily="pack://application:,,,/Fonts/#password"
  • c# code behind -> TextBox.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#password");
  • Related