首页 > 解决方案 > 从 Wpf 中的资源中的 CodeBehind 设置 FontFamily

问题描述

嗨,我想从 codebehind 更改 fontfamily,但我的字体是一种资源样式,我该怎么做?
这是我的字体字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <FontFamily x:Key="IRANSans">IRANSans, pack://application:,,,/PersianCalendar;component/Resources/Fonts/Fonts/#IRANSans</FontFamily>
</ResourceDictionary>

在 xaml 我可以这样设置字体:

FontFamily="{StaticResources IranSans}"

但我不知道如何在代码隐藏中做到这一点,我想要这样的东西

pt.FontFamily = new FontFamily(TryFindResources("IranSans"));

标签: c#wpffontsfont-family

解决方案


您可以使用var fontFamily = this.Resources["IRANSans"];


推荐阅读