Sample Font Chooser

[ Reposting, as this entry got deleted accidentally. Aplogies if you've seen this before! ] 

One thing we didn’t have time to include in the first release of WPF is a set of predefined dialogs analogous to the Win32 common dialogs. Attached to this article is a sample application that might help you implement your own font chooser.

This is not the first font chooser posted to this blog. Norris posted a XAML-only version in June, which was an impressive demonstration of how much can be accomplished in just a few lines of XAML with data binding. This version is not as admirably brief but is more full-featured, including not just family and typeface but text decorations, font information, and all the typographic properties. Here is a screen shot:

Sample font dialog screen shot

Note that you could get away with using the Win32 common dialogs for file-open, etc., but fonts are different because the font selection model is different in WPF than in GDI. For a good example of this, look at the Arial font family in the sample font chooser and in Notepad’s font dialog box.

In WPF (i.e., the sample font chooser), Arial includes the typefaces Regular, Italic, Bold, Bold Italic, Narrow, and Black, plus the simulated faces Oblique, Bold Oblique, Narrow Oblique, and Black Oblique. In GDI (i.e., Notepad’s font dialog box), Arial includes just Regular, Italic, Bold, and Bold Italic. The narrow faces are treated as a separate font family (“Arial Narrow”) because GDI doesn’t allow members of a font family to be differentiated by width. Likewise, the black faces are treated as a separate font family (“Arial Black”) because GDI supports only two weights with a family: bold and not-bold.

The attached sample code is provided as is. I won't promise it's perfect, but I hope it helps you get started. Enjoy!

--Niklas Borson