如何将自定义字体添加到我的 WindowsTemplate Studio (WPF) 项目?

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

如何将自定义字体添加到我的 WindowsTemplate Studio (WPF) 项目?

字体放在自己的文件夹中,这是我的 App.xaml 代码:

<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>

谢谢!

我已经了解了它对我的作用:

  1. 将字体添加到“字体”文件夹

  2. yourFont.ttf(右键单击)-> 属性:

  • 生成操作 -> 资源
  • 复制到输出目录 -> 不复制
  1. 在代码中使用字体:
  • xaml -> FontFamily="pack://application:,,,/Fonts/#password"
  • c# 代码隐藏 -> TextBox.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#password");