【发布时间】:2009-09-21 08:26:47
【问题描述】:
如何在 App.xaml 中为应用程序设置 FontFamily 和 FontSize?
【问题讨论】:
标签: wpf font-family
如何在 App.xaml 中为应用程序设置 FontFamily 和 FontSize?
【问题讨论】:
标签: wpf font-family
我发现了 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 中以两种方式进行操作。
首先,您为 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>
您可以设置系统字体:
./#Segoe UI
虽然我可能不会推荐这个。
【讨论】:
<Application.Resources>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="PalatineLinoType" />
</Style>
</Application.Resources>
【讨论】: