Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Christopher Bennage

Our WPF book is now available!


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?


Published Feb 03 2008, 01:19 AM by Christopher Bennage
Filed under: ,

Comments

Christopher Bennage said:

Oh yeah, I meant to add that I used Kaxaml for the bulk of the development and Expression Blend for the animations.

# February 3, 2008 1:23 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 3, 2008 1:25 AM

Pages tagged "tutorial" said:

Pingback from  Pages tagged "tutorial"

# February 3, 2008 6:37 AM

» Daily Bits - February 4, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats. said:

Pingback from  » Daily Bits - February 4, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.

# February 4, 2008 8:16 AM

About Christopher Bennage

Christopher is a software developer and consultant at Blue Spire Consulting, a company he co-founded with Rob Eisenberg in 2006. His interests include programming, liberal education, truth, beauty, and number of deceased British authors (C. S. Lewis, G. K. Chesterton, and most recently Owen Barfield.) He lives in Tallahassee, FL with his wife and three children and still prefers to play as the Night Elves in WarCraft 3. Check out Devlicio.us!

Red-Gate!