如何在App.xaml中为应用程序设置FontFamily和FontSize?
Chr*_*isF 12
我找到了David Padbury从2008年开始的一篇博文,其中介绍了如何从代码中更改它.基本上,您覆盖元数据属性,这些属性将您的更改合并到现有值中.
TextElement.FontFamilyProperty.OverrideMetadata(
typeof(TextElement),
new FrameworkPropertyMetadata(
new FontFamily("Comic Sans MS")));
TextBlock.FontFamilyProperty.OverrideMetadata(
typeof(TextBlock),
new FrameworkPropertyMetadata(
new FontFamily("Comic Sans MS")));
Run Code Online (Sandbox Code Playgroud)
还有这个MSDN论坛帖子,它以两种方式解释了如何在XAML中完成它.
1)首先,为Control类定义"全局"样式
<Style TargetType="{x:Type Control}">
<Setter Property="FontFamily" Value="Constantia"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
然后使用该BasedOn属性将其应用于其他控件.
<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>
Run Code Online (Sandbox Code Playgroud)
2)您可以设置系统字体:
<FontFamily x:Key="{x:Static SystemFonts.MenuFontFamilyKey}">./#Segoe UI</FontFamily>
<System:Double x:Key="{x:Static SystemFonts.MenuFontSizeKey}">11</System:Double>
<FontWeight x:Key="{x:Static SystemFonts.MenuFontWeightKey}">Normal</FontWeight>
Run Code Online (Sandbox Code Playgroud)
虽然我可能不会推荐这个.