wpf 在 App.xaml 中为应用程序设置 FontFamily 和 FontSize

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1453415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:01:13  来源:igfitidea点击:

Set FontFamily and FontSize for the application in App.xaml

wpffont-family

提问by Sauron

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

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

回答by ChrisF

I've found a blog post by David Padburyfrom 2008 which goes 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.

我在2008 年找到了David Padbury一篇博客文章,其中介绍了这一点以及如何从代码中更改它。基本上,您覆盖了元数据属性,这些属性合并了您对现有值的更改。

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 postwhich explains how to do it in XAML in two ways.

还有这个MSDN 论坛帖子,它解释了如何以两种方式在 XAML 中执行此操作。

1) Firstly you define a "global" style for the Controlclass

1)首先你为Control类定义一个“全局”样式

 <Style TargetType="{x:Type Control}">
   <Setter Property="FontFamily" Value="Constantia"/>
 </Style>

and then use the BasedOnproperty to apply that to other controls.

然后使用该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>

2) You can set the system fonts:

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>

Though I probably wouldn't recommend this.

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

回答by DNNB

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