How to store FontFamily as a StaticResource?

12,609

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>
Share:
12,609
Grant H.
Author by

Grant H.

I'm a developer living and working in Raleigh, NC.

Updated on June 14, 2022

Comments

  • Grant H.
    Grant H. almost 2 years

    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>
    

    What I want to do then is something like:

    <FontFamily x:Key="MainFont">Wingdings</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>
    

    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" />
    

    How should I handle this? Thanks!

  • Grant H.
    Grant H. over 11 years
    I think my confusion arises from the top declaration, which gives me a warning about there being no default constructor for `FontFamily', but apparently that does work...
  • Chris W.
    Chris W. over 11 years
    No default constructor? haha, um, what? Paste it on here we'll get you sorted out.
  • Grant H.
    Grant H. over 11 years
    Ok, so <FontFamily x:Key="MainFontFamily">Wingdings</FontFamily> in my App.xaml gives me a little warning saying "No default constructor found", but, after seeing what you posted, I tried using it on a style, and lo and behold, it worked. Go figure, no idea why VS2012 is giving me the warning.
  • Chris W.
    Chris W. over 11 years
    Ah vs2012, I havent had time to run through its inevitable shortcomings, bug it on msconnect if it becomes an issues since that's what it sounds like. ;)