#816 – Using a Font Dialog to Select Fonts

Unlike Windows Forms, WPF does not include its own font selection dialog to allow users to pick fonts.  You can create your own dialog for this purpose, or you can use a font picker that someone else has developed.  (E.g. This one at Codeproject).

You can also just use the Windows Forms FontDialog class directly from WPF.

For example:

        <TextBlock Name="tbSomeText" Text="Little Lord Fauntleroy"
                   FontSize="16" Margin="10"/>
        <Button Content="Click to Choose" HorizontalAlignment="Center"
                Padding="10,5" Margin="10"
                Click="Button_Click"/>

In the code-behind, you use a FontDialog instance and then convert the selected font to the properties that you need in WPF.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FontDialog fd = new FontDialog();
            System.Windows.Forms.DialogResult dr = fd.ShowDialog();
            if (dr != System.Windows.Forms.DialogResult.Cancel)
            {
                tbSomeText.FontFamily = new System.Windows.Media.FontFamily(fd.Font.Name);
                tbSomeText.FontSize = fd.Font.Size * 96.0 / 72.0;
                tbSomeText.FontWeight = fd.Font.Bold ? FontWeights.Bold : FontWeights.Regular;
                tbSomeText.FontStyle = fd.Font.Italic ? FontStyles.Italic : FontStyles.Normal;
            }
        }

You’ll also need to add a reference to the System.Windows.Forms assembly.

816-001

 

816-002

 

816-003

 

816-004

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.

One Response to #816 – Using a Font Dialog to Select Fonts

  1. Pingback: Dew Drop – May 9, 2013 (#1,544) | Alvin Ashcraft's Morning Dew

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

%d bloggers like this: