1

I have a strange problem in my simple application window when trying to set font size for all controls on it. Some controls inherit the font size from parent window, another (Menu, StatusBar) don't. I expected that setting the FontSize property value for the window will propagate down the element tree. But for some controls it does not work.

Why? Is there any explanation for this? Is there any error in my code?

NOTE: There is no code behind.

MainWindow.xaml

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="340" Width="300"
        FontSize="24" >

    <StackPanel>
        <Label Content="Hello! " />
        <Menu DockPanel.Dock="Top" Margin="10">
            <MenuItem Header="File"/>
            <MenuItem Header="Edit"/>
            <MenuItem Header="View"/>
            <MenuItem Header="Help"/>
        </Menu>
        <ListBox Margin="10">
            <ListBoxItem>Chapter 1</ListBoxItem>
            <ListBoxItem>Chapter 2</ListBoxItem>
        </ListBox>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Margin="5" Padding="5" Content="Help"/>
            <Button Margin="5" Padding="5" Content="OK" />
        </StackPanel>
        <StatusBar Margin="10">
            <Label>Status Bar</Label>
            <Separator/>
            <Label>Zoom</Label>
            <ComboBox SelectedIndex="0">
                <ComboBoxItem>100%</ComboBoxItem>
                <ComboBoxItem>75%</ComboBoxItem>
                <ComboBoxItem>50%</ComboBoxItem>
                <ComboBoxItem>25%</ComboBoxItem>
            </ComboBox>
        </StatusBar>
    </StackPanel>
</Window>

On the picture below the menu and the status bar are not inherit the font size:

enter image description here

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • Does this answer your question? [WPF global font size](https://stackoverflow.com/questions/893428/wpf-global-font-size) – Pavel Anikhouski Dec 03 '19 at 17:59
  • @Pavel Anikhouski - Actually, not. I know this approach. But I want to understand why parents set a property, but a child who has the same property does not inherit it, although it does not redefine it. I would prefer don't to create a hundred new styles, but to use the existing WPF mechanism for inheriting properties. – Jackdaw Dec 03 '19 at 18:18

1 Answers1

2

Some WPF controls like Menu and StatusBar explicitly set the FontSize property in their default styles.

Menu.xaml

<Style x:Key="{x:Type Menu}" TargetType="{x:Type Menu}">
    ...
    <Setter Property="FontSize"
        Value="{DynamicResource {x:Static SystemFonts.MenuFontSizeKey}}"/>
    ...
</Style>

This is why inheritance is breaking.

If a property is set explicitly it will not inherit a value.

The only way around this is to override the default styles.

Keithernet
  • 2,349
  • 1
  • 10
  • 16
  • This make sense. Disadvantage of changing style for defined type that it will touch of all elements tree. I wanted to change property only part of the element tree. Thank you. – Jackdaw Dec 03 '19 at 20:50