如何设置一个WPF应用程序的默认字体? [英] How to set the default font for a WPF application?
问题描述
我希望能够定义一个字体家族对我的WPF应用程序。最好使用资源字典从的App.xaml
引用的主题。我试着创建一个风格
如下:
I want to be able to define a font family for my WPF application. Preferably using a resource dictionary as a theme referenced from App.xaml
. I've tried creating a Style
as follows:
<Style TargetType="{x:Type Control}">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
但是,这并不正常工作。类型设置为的TextBlock
适用于大多数控制,但在有些情况下,这并不适用于一些控件。
But this doesn't work. Setting the type to TextBlock
works for most controls but there are a few controls where this doesn't apply.
我知道你可以设置字体的窗口上有一个窗口的所有子控件继承的字体。但我认为,任何对话窗口会回到默认的字体,这不正是我想要的。
I know you can set the font on a window and have all child controls of that window inherit the font. But I think any dialog windows will go back to the default font, which is not exactly what I want.
任何想法?
推荐答案
假设你的窗口
子类不重写 DefaultStyleKey
,你可以简单地把它添加到你的窗口风格,因为 TextElement.FontFamilyProperty
是一种遗传属性:
Assuming your Window
subclasses don't override DefaultStyleKey
, you can simply add it to your Window style, since TextElement.FontFamilyProperty
is an inherited property:
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
您还需要以下的的InitializeComponent <后添加到您的应用程序的构造/ code>电话:
You also need to add the following to your App constructor after the InitializeComponent
call:
FrameworkElement.StyleProperty.OverrideMetadata(typeof(Window), new FrameworkPropertyMetadata
{
DefaultValue = FindResource(typeof(Window))
});
工作原理:应用程序对象完成初始化后,其中指定的窗口样式成为默认风格对所有窗口。
How it works: After the App object finishes initializing, the Window style specified therein is made the default style for all windows.
这篇关于如何设置一个WPF应用程序的默认字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!