Charles Petzold

Button Templates

January 30, 2006
New York City

The WPF documentation includes sample templates for the common controls (located at WinFX Development / Windows Presentation Foundation / User Interface / Control Customization / Overviews and online here) but it's made clear that these are "examples."

Of course, it's possible to view the actual templates of the WPF controls just be accessing the Template property and converting it to XAML. The DumpButtonTemplate.cs program and DumpButtonTemplate.csproj project file shows how. (For some reason, the Template property of a control is null until the control has a parent, so that's why the Content property of the window is set first to the Button and then to a TextBox that displays the XAML.) The ButtonChrome class that the template makes use of is a Decorator derivative located in the Microsoft.Windows.Themes namespace.

Although control templates are commonly done in XAML, it's also possible to do them in code, and I demonstrated with the ListBox example yesterday. The BuildButtonFactory.cs program and BuildButtonFactory.csproj project file are from Chapter 11 of my forthcoming book.