Partager via


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

FontDialogSample.zip

Comments

  • Anonymous
    February 02, 2008
    The very first application I attempted to write in WPF was a utility to help me with choosing fonts.

  • Anonymous
    February 02, 2008
    The very first application I attempted to write in WPF was a utility to help me with choosing fonts.

  • Anonymous
    March 05, 2008
    This is really neat.  I downloaded it, compiled it, and it works!  I am a happy camper!

  • Anonymous
    April 15, 2008
    The comment has been removed

  • Anonymous
    September 25, 2009
    It it amazing there is no Font container object to store all those properties: {StandardLigatures} {ContextualLigatures} {DiscretionaryLigatures} {HistoricalLigatures} {ContextualAlternates} {HistoricalForms} {Kerning} {CapitalSpacing} {CaseSensitiveForms} {StylisticSet1} {StylisticSet2} {StylisticSet3} {StylisticSet4} {StylisticSet5} {StylisticSet6} {StylisticSet7} {StylisticSet8} {StylisticSet9} {StylisticSet10} {StylisticSet11} {StylisticSet12} {StylisticSet13} {StylisticSet14} {StylisticSet15} {StylisticSet16} {StylisticSet17} {StylisticSet18} {StylisticSet19} {StylisticSet20} {SlashedZero} {MathematicalGreek} {EastAsianExpertForms} {Fraction} {Variants} {Capitals} {NumeralStyle} {NumeralAlignment} {EastAsianWidths} {EastAsianLanguage} {AnnotationAlternates} {StandardSwashes} {ContextualSwashes} {StylisticAlternates} {SelectedFontFamily} {SelectedFontWeight} {SelectedFontStyle} {SelectedFontStretch} {SelectedFontSize} {SelectedTextDecorations}

  • Anonymous
    January 11, 2010
    perfect! I want to learn more about it.

  • Anonymous
    July 25, 2013
    The comment has been removed

  • Anonymous
    July 16, 2014
    找不到 Draft.PresentationDesignMarkup, Version=0.0.4030.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL