tags:

views:

1768

answers:

3

I'm creating a WPF app and I would like to know the best way to be able to change the font size for every element in the ui. Do I create a resource dictionary and set Styles to set the font size for all the controls I use?

What is the best practice?

+2  A: 

For any styles in WPF, you should have a separate resource dictionary that contains the styles for your app.

If you want to have a single Font Size that's reused throughout the app then just create a style for that font size. You can either give it a unique name/key to use explicitly or you can set a targetType that will transcend throughout the app.

Explicit Key:

<Style
    x:Key="MyFontSize"
    TargetType="TextBlock">
    <Setter
        Property="FontSize"
        Value="10" />
</Style>

<Control
    Style="{StaticResource MyFontSize}" />

*Note this style can be used with controls that have contentPresenters

For all textblocks in the app:

<Style
    TargetType="TextBlock">
    <Setter
        Property="FontSize"
        Value="10" />
</Style>

<TextBlock
        Text="This text will be size 10" />
Chris Nicol
+4  A: 

I'd do it this way:

<Window.Resources>
     <Style TargetType="{x:Type Control}" x:Key="baseStyle">
      <Setter Property="FontSize" Value="100" />
     </Style>
     <Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"></Style>
     <Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"></Style>
     <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource baseStyle}"></Style>
     <Style TargetType="{x:Type ListView}" BasedOn="{StaticResource baseStyle}"></Style>
     <!-- ComboBox, RadioButton, CheckBox, etc... -->
    </Window.Resources>

That way, if I want to change ALL the controls, I'd just have to change the "baseStyle" style, the rest would just inherit from it. (That's what BasedOn property those, you can also extend the base style if you create other setters inside of the inherited style)

Carlo
And if you need to change the font size on the fly, make sure that all of them are declared to DynamicResource, both in the Styles and in the Style references to "baseStyle".
Jeff Wain
Seroiouslly? I tried doing that and I get this error: Property 'BaseOn' does not support values of type 'DynamicResourceExtension'. I just changed the code from StaticResource to DynamicResource. What do you think is missing?
Carlo
Forgot about that little issue. We have all our declarations set up as Setters declared per-control so that we don't have huge hierarchies. This post explains it pretty well.http://stackoverflow.com/questions/585429/using-basedon-style-property-on-dynamicresources
Jeff Wain
I got a little curious about this issue and I went ahead to do some tests, but it seems like once you've set and are using a StaticResource, you can't modify it. Maybe a different solution is needed if you want to change the size at some point in your app.
Carlo
A: 

FontSizeProperty is inherited from Parent Control. So you just need to change FontSize of your main window.

If you don't need dynamic behaviour this should work:

Add a style for Window to your ResourceDictionary

<Style TargetType="{x:Type Window}">
     <Setter Property="FontSize" Value="15" />
</Style>

Apply the style to your main form (will not be applied implicit because its a derived type)

 Style = (Style)FindResource(typeof (Window));
Matze