【问题标题】:Set FontFamily and FontSize for the application in App.xaml在 App.xaml 中为应用程序设置 FontFamily 和 FontSize
【发布时间】:2009-09-21 08:26:47
【问题描述】:

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

【问题讨论】:

    标签: wpf font-family


    【解决方案1】:

    我发现了 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 forum post,它解释了如何在 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 11 正常

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

    【讨论】:

      【解决方案2】:
      <Application.Resources>
           <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
                  <Setter Property="FontFamily" Value="PalatineLinoType" />
           </Style>
      </Application.Resources>
      

      【讨论】:

        猜你喜欢
        • 2011-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多