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

发布于 2024-08-05 11:46:35 字数 51 浏览 14 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆凄凉。 2024-08-12 11:46:35

我找到了 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 类定义“全局”样式

,然后使用 BaseOn 属性将其应用到其他控件。

<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
    11
    正常

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

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.

  1. 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>
  1. 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.

花桑 2024-08-12 11:46:35
<Application.Resources>
     <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="PalatineLinoType" />
     </Style>
</Application.Resources>
<Application.Resources>
     <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="FontFamily" Value="PalatineLinoType" />
     </Style>
</Application.Resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文