How to save FontFamily as StaticResource?

I am trying to figure out how to install FontFamily in my App.xaml so that I can declaratively apply this style wherever I need. In ResourceDictionary I can apply something like:

 <System:Double x:Key="SmallTextSize">10</System:Double> 

What I want to do is something like:

 <FontFamily x:Key="MainFont">Wingdings</FontFamily> 

But the only thing I can get is an implicit style that requires a target, and a few font declarations that I want to use. I need to be able to apply a style in which I get the FontFamily property of any control.

Here is the closest I can come now:

 <System:String x:Key="MainFont">Wingdings</System:String> <Style TargetType="UserControl"> <Setter Property="FontFamily" Value="{StaticResource MainFont}"></Setter> </Style> 

This implementation does not work on something like because it expects MainFont to be FontFamily, not a string:

 <TextBlock Text="{Binding}" Margin="0,0,0,4" FontWeight="Normal" FontFamily="{StaticResource MainFont}" FontSize="14.667" /> 

How can I do it? Thanks!

+6
source share
1 answer

Not sure if I fully understand this for sure, as I do this:

 <FontFamily x:Key="MainFont">WingDings</FontFamily> 

If you are talking about applying it to multiple instances without having to declare them to everyone, then I would just like to:

 <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> 
+11
source

All Articles