Scenario:
I want to use 3 standard font sizes for my WPF application: BigFontSize , NormalFontSize and SmallFontSize . These are double values, and they are defined in the resource dictionary as (where sys defined accordingly):
<sys:Double x:Key="BigFontSize">18</sys:Double> <sys:Double x:Key="NormalFontSize">14</sys:Double> <sys:Double x:Key="SmallFontSize">12</sys:Double>
It works well. But I accidentally chose 14 as the normal size. I want to get the system font size for NormalFontSize . (If this is done, I can use the converter as described here to get BigFontSize and SmallFontSize relative to NormalFontSize )
Clue:
I found in the documentation that the default font size is stored in the static (double) SystemFonts.MessageFontSize . But what should I do to get this value in the resource dictionary? (I know that Binding or DynamicResource cannot be applied. But hey, this is a static value, since I can use StaticResource or x:Static or something else?)
I 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 them do not seem to work (as expected). I get a Cannot add content to object of type 'System.Double'. error message Cannot add content to object of type 'System.Double'.
Note:
- I do not want to do this from code, for example, from application (). (Is it possible to have code for ResourceDictionary?)
I don’t want to encapsulate this in a generalized style, from which other styles can be derived (using BasedOn ), because I have several Resource Dictionaries, and it is impossible to use DynamicResource BasedOn
That is, I can’t 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 another ResourceDictionary, say HeaderTextBlockStyle , then I have to use BasedOn={DynamicResource BigFont} , which is impossible (I think)
Any help would be greatly appreciated. Thanks.
TAGS : WPF SystemFonts.MessageFontSize ResourceDictionary FontSize BasedOn DynamicResource
mg007 source share