从WPF中资源的CodeBehind设置FontFamily

时间:2019-06-02 07:23:58

标签: c# wpf fonts font-family

嗨,我想从代码背后更改fontfamily,但是我的字体是一种资源样式 我怎样才能做到这一点?
这是我的fontDictionary

<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"));

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用Application.Current.Resources

访问应用程序资源

尝试一下:

pt.FontFamily = Application.Current.Resources["IRANSans"] as FontFamily;
相关问题