(WPF) 如何从 ResourceDictionary 将 sys:Double 的值设置为 SystemFonts.MessageFontSize?
场景:
我想为我的 WPF 应用程序使用 3 种标准字体大小:BigFontSize
、NormalFontSize
和 SmallFontSize
。这些是双精度值,它们在资源字典中定义为(其中 sys
被适当定义):
<sys:Double x:Key="BigFontSize">18</sys:Double>
<sys:Double x:Key="NormalFontSize">14</sys:Double>
<sys:Double x:Key="SmallFontSize">12</sys:Double>
这效果很好。但我随机选择了14作为正常尺寸。我想要的是获得系统定义的 NormalFontSize
字体大小。 (如果完成了,我可以使用转换器如上所述此处获取相对于NormalFontSize
的BigFontSize
和SmallFontSize
)
线索:
我从文档中发现默认字体大小存储在静态属性(双精度)SystemFonts.MessageFontSize
中。但是我应该怎么做才能将该值检索到资源字典中? (我知道无法应用 Binding
或 DynamicResource
。但是,嘿,这是一个静态值,那么我如何应用 StaticResource
或 x:Static
或其他什么?)
我已经尝试过
<sys:Double x:Key="XXXFontSize">
<StaticResource ResourceKey="SystemFonts.MessageFontSize" />
</sys:Double>
,但
<sys:Double x:Key="XXXFontSize">
<x:Static ResourceKey="SystemFonts.MessageFontSize" />
</sys:Double>
两者似乎都不起作用(如预期)。我收到一条错误消息 Cannot add content to object of type 'System.Double'.
注意:
- 我不想从代码中执行此操作,例如从 App() 。 (是否可以有 ResourceDictionary 的代码隐藏?)
我不想将其封装在可以派生其他样式的通用样式中(使用
BasedOn
),因为我有几个资源字典,并且无法将DynamicResource
与BasedOn
一起使用
也就是说,我不能使用<样式 x:Key="BigFont" TargetType="{x:Type Control}"}>
因为,如果我在其他 ResourceDictionary 中有样式,例如
HeaderTextBlockStyle
,那么我必须使用BasedOn={DynamicResource BigFont}
这是不可能的(我认为)
任何帮助将不胜感激。
谢谢。
标签:WPF SystemFonts.MessageFontSize ResourceDictionary FontSize BasedOn DynamicResource
Scenario:
I want to use 3 standard font size for my WPF application : BigFontSize
, NormalFontSize
, and SmallFontSize
. These are double values and they are defined in resource dictionary as (where sys
is appropriately defined):
<sys:Double x:Key="BigFontSize">18</sys:Double>
<sys:Double x:Key="NormalFontSize">14</sys:Double>
<sys:Double x:Key="SmallFontSize">12</sys:Double>
This works well. But i have randomly selected 14 as a normal size. What i want is to get a system defined font size for NormalFontSize
. (If that's done, I can use a converter as described here to get BigFontSize
and SmallFontSize
relative to NormalFontSize
)
Clue :
I found from the documentation that default font size is stored in a static property (double) SystemFonts.MessageFontSize
. But what should I do to retrieve that value to resource dictionary? (I know Binding
or DynamicResource
cannot be applied. But hey, this is a static value, so how can I apply StaticResource
or x:Static
or whatever?)
I have tried
<sys:Double x:Key="XXXFontSize">
<StaticResource ResourceKey="SystemFonts.MessageFontSize" />
</sys:Double>
and
<sys:Double x:Key="XXXFontSize">
<x:Static ResourceKey="SystemFonts.MessageFontSize" />
</sys:Double>
Both of which doesn't seem to work (as expected). I get an error saying Cannot add content to object of type 'System.Double'.
Note:
- I don't want to do this from code, e.g from App(). (Is it possible to have a code-behind for ResourceDictionary?)
I don't want to encapsulate this in generalized style from which other styles can be derived (using
BasedOn
) because I have several Resource Dictionaries, and it'll not be possible to useDynamicResource
withBasedOn
That is, I cannot use<Style x:Key="BigFont" TargetType="{x:Type Control}"}> <Setter Property="Control.FontSize" Value="{Binding Source={x:Static SystemFonts.MessageFontSize}, Converter={ . . . }" /> </Style>
Because, if I have a style in other ResourceDictionary, say
HeaderTextBlockStyle
, then I would have to useBasedOn={DynamicResource BigFont}
which is not possible (I think)
Any help would be greatly appreciated.
Thank you.
TAGS : WPF SystemFonts.MessageFontSize ResourceDictionary FontSize BasedOn DynamicResource
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经这样做了......
并且它的工作就像一个奇迹!我可以在同一个(部分)资源字典中或像这样的其他资源字典中使用这些资源...
我不知道这是否是一个“良好实践”(请对此发表评论),我只知道它作品..!!!
I've done like this...
... and it's working like like a miracle!!! I can use these resources in the same (partial) resource dictionary or from other resource dictionaries like this...
I don't know if it is a "good practice" or not (please comment on this), I only know that it works..!!!