如何将 FontFamily 存储为静态资源? [英] How to store FontFamily as a StaticResource?

查看:25
本文介绍了如何将 FontFamily 存储为静态资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何在我的 App.xaml 中设置 FontFamily,以便我可以在任何需要的地方声明性地应用该样式.在 ResourceDictionary 中,我可以应用以下内容:

I'm trying to figure out how to set a FontFamily in my App.xaml in such a way that I can declaratively apply that style wherever I need to. In the ResourceDictionary I can apply something like:

<System:Double x:Key="SmallTextSize">10</System:Double>

然后我想做的是:

<FontFamily x:Key="MainFont">Wingdings</FontFamily>

但是,我唯一可以开始工作的是隐式样式,它需要一个目标,以及我想要使用的字体的多个声明.我需要能够将我最终得到的样式应用于任何控件的 FontFamily 属性.

But, the only thing I can get to work is an implicit style, which requires a target, and multiple declarations of the font I want to use. I need to be able to apply the style I end up with to the FontFamily property of any control.

这是我目前能到达的最近的地方:

Here's the closest I can come presently:

<System:String x:Key="MainFont">Wingdings</System:String>
<Style TargetType="UserControl">
      <Setter Property="FontFamily" Value="{StaticResource MainFont}"></Setter>
</Style>

这个实现不适用于类似的东西,因为它期望 MainFont 是一个 FontFamily,而不是一个字符串:

This implementation doesn't work on something like because it expects MainFont to be a FontFamily, not a string:

<TextBlock Text="{Binding}" Margin="0,0,0,4" FontWeight="Normal" FontFamily="{StaticResource MainFont}" FontSize="14.667" />

我该如何处理?谢谢!

推荐答案

不确定我是否完全理解这个,因为我所做的是;

Not sure I entirely understand this one exactly, since what I do is;

<FontFamily x:Key="MainFont">WingDings</FontFamily>

如果您说的是将其应用于多个实例而不必向每个实例声明它,那么我会这样做;

If you're talking about then applying it to multiple instances without having to declare it to each one then I would just do like;

<Object>
   <Object.Resources>
      <Style TargetType="TextBlock" BasedOn="{StaticResource YourDefaultTextBlockStyleToInheritOtherProperties}">
         <Setter Property="FontFamily" Value="{StaticResource MainFont}"/>
      </Style>
   </Object.Resources>

   <!-- Your FontFamily automatically gets inherited to all children of the object 
        whether your object is say a Grid, or StackPanel, 
        or even an entire UserControl -->    
   <TextBlock Text="ABCDEFG"/>
   <TextBlock Text="12345"/>
   <TextBlock Text="!()*&@#"/>

</Object>

这篇关于如何将 FontFamily 存储为静态资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆