#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.

About these ads

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 365 other followers

%d bloggers like this: