Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting global font family [duplicate]

Tags:

wpf

xaml

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;
    }
like image 786
gcso Avatar asked Jul 07 '11 18:07

gcso


People also ask

How do I set the global font-family?

To set a global font family in React, set the font-family style on the html element in your index. css file and import the file in your index. js file. Global CSS should be imported in index.

How do I change my font globally?

Applying a Global Font ChangeUnder Change all fonts with typeface and size, in the left box, select the existing font you want to change. In the right box, select the size of font you want to use. Select All sizes if you want to replace all instances of the font, regardless of size.

Can multiple font families be specified to font-family property?

Yes. The font-family property can hold several font names as a "fallback" system.

How do I make all fonts the same in HTML?

You can use a <basefont> tag to set all of your text to the same size, face, and color. The font tag is having three attributes called size, color, and face to customize your fonts. To change any of the font attributes at any time within your webpage, simply use the <font> tag.


1 Answers

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}"/>
like image 145
CodeWarrior Avatar answered Oct 26 '22 07:10

CodeWarrior