Set FontFamily and FontSize for the application in App.xaml
Asked Answered
A

2

10

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

Atrophy answered 21/9, 2009 at 8:26 Comment(0)
L
13

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.

Lycopodium answered 21/9, 2009 at 8:28 Comment(0)
S
3
<Application.Resources>
     <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="PalatineLinoType" />
     </Style>
</Application.Resources>
Sunbreak answered 5/2, 2011 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.