在App.xaml中设置应用程序的字体系列(FontFamily)和字体大小(FontSize)。

10

如何在App.xaml中设置应用程序的字体和字号?

2个回答

13

我发现了一篇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")));

还有这篇MSDN论坛帖子,其中提供了两种在XAML中实现的方法。

  1. 首先,您需要为Control类定义一个“全局”样式。

然后使用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>
  1. 你可以设置系统字体:

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

虽然我可能不建议这样做。


3
<Application.Resources>
     <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="PalatineLinoType" />
     </Style>
</Application.Resources>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接