Wpf 在App.xaml中为应用程序设置FontFamily和FontSize
如何在App.xaml中设置应用程序的FontFamily和FontSize?我找到了David Padbury 2008年发表的一篇博文(遗憾的是,该博文已不存在),该博文讨论了这一问题以及如何从代码中更改它。基本上,您覆盖了元数据属性,这些属性合并了对现有值的更改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
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>
这是一个标签
这是一个按钮
<Application.Resources>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="PalatineLinoType" />
</Style>
</Application.Resources>