簡體   English   中英

在 App.xaml 中為應用程序設置 FontFamily 和 FontSize

[英]Set FontFamily and FontSize for the application in App.xaml

如何在 App.xaml 中為應用程序設置 FontFamily 和 FontSize?

我找到了 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> 正常

雖然我可能不會推薦這個。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM