(WPF) How to set sys: Double to SystemFonts.MessageFontSize from ResourceDictionary?

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

+4
source share
2 answers

I did like this ...

 public partial class GlobalResources : ResourceDictionary { public GlobalResources() { this.Add("GiantFontSize", SystemFonts.MessageFontSize * 2.5); this.Add("BigFontSize", SystemFonts.MessageFontSize * 1.5); this.Add("MediumFontSize", SystemFonts.MessageFontSize * 1.25); this.Add("NormalFontSize", SystemFonts.MessageFontSize); this.Add("SmallFontSize", SystemFonts.MessageFontSize * 0.85); } } 

... and it works like a miracle !!! I can use these resources in the same (partially) resource dictionary or from other resource dictionaries like this ...

 <Style ...> <Setter Property="FontSize" Value="{DynamicResource MediumFontSize}" /> ... </Style> 

I don’t know if this is “good practice” or not (comment on this), I only know that it works .. !!!

+4
source

Check out this article: Override Default Styles It may have what you are looking for.

0
source

Source: https://habr.com/ru/post/1299740/


All Articles