#337 – Specifying Font Properties for All Controls In a Window

Because the various font-related properties are dependency properties, they can be set on a high-level element in the logical tree and “trickle down” to lower-level elements.  The lower-level elements inherit property values from higher-level elements.

In the example below, we specify a font to use for all child controls within a window.

<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
	x:Class="WpfApplication11.MainWindow"
	x:Name="Window"
	Title="Quotes"
	Width="780" Height="368"
    FontFamily="Georgia">

    <StackPanel>
        <Label Content="A nice quote:" Margin="20,10,20,0"/>

        <TextBlock Name="txt2" Margin="20" FontSize="18" TextWrapping="Wrap">
            Freedom is never dear at any price. It is the breath of life. What would a man not pay for living?<LineBreak/>
            --Mohandas Gandhi
        </TextBlock>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <RadioButton Content="Like" Margin="10"/>
            <RadioButton Content="Don't Like" Margin="10"/>
        </StackPanel>

        <Button Content="OK" FontFamily="Tahoma" Width="100" Margin="20"/>
    </StackPanel>


Notice that we can override an inherited property by setting it explicitly for a control. We specify that Tahoma should be the FontFamily for the Button control.

Advertisement