Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
XAML Only WPF Example (Font Viewer)

The very first application I attempted to write in WPF was a utility to help me with choosing fonts. I'm a developer who has one foot in the designer world, and I am always looking for just the right font. I think that was about 2 years ago, and I was ecstatic that my prototype application took about 15 minutes to write and contained 3 or 4 lines of code (that's not counting the XAML markup.)

When Rob and I began outlining our upcoming book on WPF, we decided to structure the it around four example applications.  The font viewer seemed like a natural choice for the first application because it's so simple to write., and easy to understand.  I showed the source to Rob, and in less than 10 minutes we had a 100% XAML version of the application. Yes, we removed those troublesome 3-4 lines of code.  :-)

I decided to play around with it some more, and here is the result. It's a 24KB text file containing just XAML.  If you have .NET 3.5 installed, it should execute in your browser (both Firefox and Internet Explorer work).  It's a very simple application, but I think it's fun. I'm especially fond of the built-in tutorial (be sure to click the help icon).

This version is a lot more complicated than the one we build in the book.  We don't introduce the concepts of styles, templates, animation, etc until after we're done with the font viewer. 

In a nutshell, here is how it works. The list box is bound to the set of installed fonts on your system (System.Windows.Media.Fonts.SystemFontFamilies). There are series of four text blocks, whose font family is bound to the selected item on the list box.  Finally, the four text blocks get their content from the text box at the bottom. That's it.

Please feel free to analyze, dissect, assimilate as you like.  You can get to the source using your browser's "view source" feature.

Disclaimer: Only very recently did I bother to see if anyone else had done the same thing.  Yes, they had, and more.

Bonus Question: How would you handle building an application like this with TDD\BDD? How does declarative programming collide with TDD\BDD?

Posted 02-03-2008 1:19 AM by Christopher Bennage
Filed under: ,


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)