5

how can I increase font of a, let's say, TextBlock? I don't want to have something like this:

<TextBlock FontSize="20">
  text
</TextBlock>

because it won't work correctly when user changes Windows' settings of the controls' font size. Do we have something like +VALUE (eg. +2), similar to HTML?

EDIT
That's what I meant talking about the Windows' settings: enter image description here

but the answers I received totally satisfies me.

SOReader
  • 5,697
  • 5
  • 31
  • 53
  • What do you mean by "it won't work correctly when user changes Windows' settings of the controls' font size"? I don't think the user can set sizes of individual control types (there's nothing for it in the "Advanced appearance settings" dialog). They can scale all the screen contents to 125% or 150%, but WPF will respect that. So I'm not sure what setting you're thinking of that you think WPF will ignore. – Joe White Aug 15 '12 at 12:28
  • I made an edit to answer your question. – SOReader Aug 15 '12 at 13:20

4 Answers4

11

WPF doesn't have the em font size, there alternatives in the answers to this SO

The simplist may be

<ScaleTransform ScaleX="1.2" ScaleY="1.2" /> 
Community
  • 1
  • 1
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
9

Adapting Bob Vale's answer to your original code

<TextBlock>
    <TextBlock.RenderTransform>
        <ScaleTransform ScaleX="1.2" ScaleY="1.2" />
    </TextBlock.RenderTransform>
    text
</TextBlock>
Craig
  • 6,869
  • 3
  • 32
  • 52
Jeson Martajaya
  • 6,996
  • 7
  • 54
  • 56
2

First of all you should create an application scoped style for you font sizes, as described in this answer : WPF global font size

Then, you can bind the fontsize values to a property of a static class taking the size defined in control panel, and adapt it accordingly.

Community
  • 1
  • 1
mathieu
  • 30,974
  • 4
  • 64
  • 90
1

For those poor souls who find this questions in need for a relative font size mechanism for design purposes such as you'd use in css, I found a hack that appears to work in WPF.

It's used this way:

<StackPanel>
    <TextBlock>outer</TextBlock>
    <ContentControl local:RelativeFontSize.RelativeFontSize="2">
        <StackPanel>
            <TextBlock>inner</TextBlock>
            <ContentControl local:RelativeFontSize.RelativeFontSize="2">
                <StackPanel>
                    <TextBlock>innerest</TextBlock>
                </StackPanel>
            </ContentControl>
        </StackPanel>
    </ContentControl>
</StackPanel>

Which gives this:

screenshot

And here's the code:

public static class RelativeFontSize
{
    public static readonly DependencyProperty RelativeFontSizeProperty =
        DependencyProperty.RegisterAttached("RelativeFontSize", typeof(Double), typeof(RelativeFontSize), new PropertyMetadata(1.0, HandlePropertyChanged));

    public static Double GetRelativeFontSize(Control target)
        => (Double)target.GetValue(RelativeFontSizeProperty);

    public static void SetRelativeFontSize(Control target, Double value)
        => target.SetValue(RelativeFontSizeProperty, value);

    static Boolean isInTrickery = false;

    public static void HandlePropertyChanged(Object target, DependencyPropertyChangedEventArgs args)
    {
        if (isInTrickery) return;

        if (target is Control control)
        {
            isInTrickery = true;

            try
            {
                control.SetValue(Control.FontSizeProperty, DependencyProperty.UnsetValue);

                var unchangedFontSize = control.FontSize;

                var value = (Double)args.NewValue;

                control.FontSize = unchangedFontSize * value;

                control.SetValue(Control.FontSizeProperty, unchangedFontSize * value);
            }
            finally
            {
                isInTrickery = false;
            }
        }
    }
}
John
  • 6,693
  • 3
  • 51
  • 90