Take the 2-minute tour ×

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>

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!

share|improve this question
Here's a possible workaround if you don't find a more suitable answer: use an attached dependency property to set the FontFamily. You would then get something like: <TextBlock FontManager.FontFamily="{StaticResource MainFont}" />. –  odyss-jii Jan 17 '13 at 16:43
Thanks, but that's the exact opposite of what I'm going for. I'm trying to consolidate styles in a VERY large Silverlight application, so applying the styles at that level would mean thousands of edits. –  Grant H. Jan 17 '13 at 16:47
Maybe I am missing something, but if you are going to be setting fonts as in your example above, that is FontFamily="{StaticResource MainFont}", then it will require the exact same number of edits. –  odyss-jii Jan 17 '13 at 16:50
No, that is an implicit style, meaning one setting per type of object, not per object. –  Grant H. Jan 17 '13 at 16:53
Do you mean <Style TargetType="UserControl"> <Setter Property="myns:FontManager.FontFamily" Value="{StaticResource MainFont}"></Setter> </Style>? You can apply attached properties in the implicit style as well. –  odyss-jii Jan 17 '13 at 16:54

1 Answer 1

up vote 5 down vote accepted

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;

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

   <!-- 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="!()*&@#"/>

share|improve this answer
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... –  Grant H. Jan 17 '13 at 16:58
No default constructor? haha, um, what? Paste it on here we'll get you sorted out. –  Chris W. Jan 17 '13 at 17:00
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. –  Grant H. Jan 17 '13 at 17:02
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. ;) –  Chris W. Jan 17 '13 at 17:03

Your Answer


