Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在App.xaml中为应用程序设置FontFamily和FontSize_Wpf_Font Family - Fatal编程技术网

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

Wpf 在App.xaml中为应用程序设置FontFamily和FontSize,wpf,font-family,Wpf,Font Family,如何在App.xaml中设置应用程序的FontFamily和FontSize?我找到了David Padbury 2008年发表的一篇博文(遗憾的是,该博文已不存在),该博文讨论了这一问题以及如何从代码中更改它。基本上,您覆盖了元数据属性,这些属性合并了对现有值的更改 TextElement.FontFamilyProperty.OverrideMetadata( typeof(TextElement), new FrameworkPropertyMetadata( new FontFa

如何在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")));
这也解释了如何用两种方式在XAML中实现它

  • 首先,为
    控件定义一个“全局”样式

  • 然后使用该属性将其应用于其他控件

    <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>
    
    
    这是一个标签
    这是一个按钮
    
  • 您可以设置系统字体:

    ./#Segoe用户界面 11 正常的

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

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