10

如何在 App.xaml 中为应用程序设置 FontFamily 和 FontSize?

4

2 回答 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> 正常

虽然我可能不会推荐这个。

于 2009-09-21T08:28:54.267 回答
3
<Application.Resources>
     <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="PalatineLinoType" />
     </Style>
</Application.Resources>
于 2011-02-05T17:13:04.407 回答