How can I set the FontFamily and FontSize for the application in App.xaml?
Set FontFamily and FontSize for the application in App.xaml
Asked Answered
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.
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>
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.
<Application.Resources>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="PalatineLinoType" />
</Style>
</Application.Resources>
© 2022 - 2024 — McMap. All rights reserved.