Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set FontFamily and FontSize for the application in App.xaml

How can I set the FontFamily and FontSize for the application in App.xaml?

like image 982
Sauron Avatar asked Sep 21 '09 08:09

Sauron


2 Answers

I've found a blog post by David Padbury from 2008 (sadly no longer in existence) which went into this and how to change it from code. Basically you override the meta data properties which merges in your changes to the existing values.

TextElement.FontFamilyProperty.OverrideMetadata(
typeof(TextElement),
new FrameworkPropertyMetadata(
    new FontFamily("Comic Sans MS")));

TextBlock.FontFamilyProperty.OverrideMetadata(
typeof(TextBlock),
new FrameworkPropertyMetadata(
    new FontFamily("Comic Sans MS")));

There's also this MSDN forum post which explains how to do it in XAML in two ways.

  1. Firstly you define a "global" style for the Control class

and then use the BasedOn property to apply that to other controls.

<StackPanel   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <StackPanel.Resources>
  <Style TargetType="{x:Type Control}" x:Key="ControlStyle">
     <Setter Property="FontFamily" Value="Constantia"/>
   </Style>

  <Style TargetType="{x:Type Label}" x:Key="LabelStyle" BasedOn="{StaticResource ControlStyle}">
   <Setter Property="FontWeight" Value="Bold" />
  </Style>
        <Style TargetType="{x:Type Button}" x:Key="ButtonStyle" BasedOn="{StaticResource ControlStyle}">
         <Setter Property="Background" Value="Blue"/>
  </Style>
 </StackPanel.Resources>

 <Label Style="{StaticResource LabelStyle}">This is a Label</Label>
 <Button Style="{StaticResource ButtonStyle}">This is a Button</Button>
</StackPanel>
  1. You can set the system fonts:

    ./#Segoe UI <System:Double x:Key="{x:Static SystemFonts.MenuFontSizeKey}">11</System:Double> Normal

Though I probably wouldn't recommend this.

like image 135
ChrisF Avatar answered Oct 15 '22 20:10

ChrisF


<Application.Resources>
     <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="PalatineLinoType" />
     </Style>
</Application.Resources>
like image 25
DNNB Avatar answered Oct 15 '22 21:10

DNNB