tags:

views:

239

answers:

2

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>

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.

Any ideas?

Thanks.

+2  A: 

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> 

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 sepecified therein is made the default style for all windows.

Ray Burns
This does not work because the Style is not applied to types that are inherited form Window.
Matze
@Matze: It works for me. Perhaps you accidentally overrode DefaultStyleKey or explicitly set a window style?
Ray Burns
@Ray: Your snippet definitely does not work. Because you have to use a class derived from Window, some additional code and/or xal is involved.
Matze
@Matze: Try it, it works! Do this: 1. Create a new WPF Application in Visual Studio. 2. Add the code above to your Application.Resources. 3. Add a Button to Window1 with text content. 4. Run. You will see the font has changed (this is more obvious if you use Value="Times New Roman"). 5. Add window Window2 with another Button. 6. Add "new Window2().Show()" to Window1's constructor. 7. Run again. You will see that both windows pick up the new font. 8. Figure out what's different between your code and this simple example you'll know where you're going wrong.
Ray Burns
@Ray: Does not work. Just created the example as you suggested. The Style is only applied to the designer view. Maybe you something like <Style TargetType="{x:Type local:MainWindow}">
Matze
@Matze: This was very puzzling because what I wrote in my answer is *exactly* the code I use in my production applications. I finally figured out why it worked for me but not for you and have updated my answer to correct the problem. Thank you very much for bringing this to my attention.
Ray Burns
Or inside the protected override void OnStartup(StartupEventArgs e) of your Application class. Because you need to create your own Main method to directly call InitializeComponent of App. Thanks for your efforts to help!
Matze
Actually I don't have my own Main method. I simply do it in the App object's constructor. All I write is `public App() { InitializeComponent(); FrameworkElement.StyleProperty.OverrideMetadata(...); }`. This seems cleaner to me than putting it in OnStartup.
Ray Burns
A: 

I found a acceptable solution at this link:

http://davidpadbury.co.uk/2008/08/30/change-the-default-font-of-a-wpf-application/

Pileggi

pileggi