Setting global font family [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

Possible Duplicate:
How to set the default font for a wpf application?

Coming from a web development background I often get mixed up on how to assign styles to my controls. What I'd like to do is set a global font family for the entire application.

In my App.xaml file I have the following

    <Style TargetType="{x:Type Window}">         <Setter Property="FontFamily" Value="Helvetica, Arial" />         <Setter Property="FontSize" Value="13" />     </Style> 

I've tried changing the target type to Control but that's not doing anything. I would expect that since everything technically lives in a Window control that everything would work as expected.

Turns out the TextBlock control doesn't inherit from Control. I'm assuming that's most of the problem because 90% of my text is in TextBlock form.

In CSS I would do something like this:

body {     font-family: Helvetica, Arial;     font-size: 13px;     } 

回答1:

What I have previously done is create a Style for Control, then derive other control styles from it. For whatever reason, WPF doesn't want to take a FontFamily setting directly from a Control style, but if it takes it from a Button Style that is based on Control, then it works. If I get some time later on, I will dig around and find a previous implementation of this.

Edit:
Couldn't remember where I might have put a ready made example, so I made one:

<Style x:Key="ControlStyle" TargetType="Control">     <Setter Property="FontFamily" Value="Wingdings"/> </Style> <Style TargetType="Button" BasedOn="{StaticResource ControlStyle}"/> 

Also, keep in mind, a style for TextBlock cannot be based on Control. Textblock derives from Framework element, not Control. Label, checkbox, Textbox, etc derive from Control which derives from Framework Element.

You will likely have to have a separate style for Textblock. One thing you can do is set a font family resource and bind your top level styles to that. Then if it changes, all you have to do is change that one instance.

<FontFamily x:Key="DefaultFont" >Ravie</FontFamily>  <Setter Property="FontFamily" Value="{DynamicResource DefaultFont}"/> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!