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

Christopher Bennage

Our WPF book is now available!


Identifying Control Templates Parts in WPF

I'm currently writing the chapter about control templates in our WPF book, and I wanted to include a list of all of the named parts that are hidden away in the dark recesses of the various controls.  Before I even started googling, I had an idea that I thought I'd share.

For those of you not yet familiar with control templates, WPF allows you to redefine the visual appearance of controls. The problem is that some of the controls have special named parts, that is, there are visual elements with specific names that the code expects to be there.  Luckily, there is the TemplatePartAttribute that's meant to decorate controls with named parts. If you look at the documentation for a given control, the attributes are listed and you'll know what named elements a new template requires.

Given an assembly though, it's quite easy to identify the controls programmatically. Here's some C# that does just that:

using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

namespace TemplatesParts
{
    class Program
    {
        static void Main()
        {
            var assembly = Assembly.GetAssembly(typeof (Control));
            var attributeType = typeof (TemplatePartAttribute);

            var controls = from c in assembly.GetTypes()
                           where c.GetCustomAttributes(attributeType, false).Length > 0
                           orderby c.Name
                           select new {c.Name,Parts=c.GetCustomAttributes(attributeType, false)};

            foreach (var control in controls)
            {
                Console.WriteLine(control.Name);
                foreach (var part in control.Parts)
                {
                    Console.WriteLine( "   " + ((TemplatePartAttribute)part).Name);
                }
            }
            Console.ReadLine();
        }
    }
}

Now, if I can just look this up in the documentation, why bother with this code? It was fun, what can I say?



Comments

Christopher Bennage said:

Oh yeah, I was running that as a console (duh) with references to PresentationCore, PresentationFramework, and WindowsBase.

I'm still new to Linq though, so if any has a suggestion there I'd love to hear it.

# March 9, 2008 9:32 PM

Dew Drop - March 10, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - March 10, 2008 | Alvin Ashcraft's Morning Dew

# March 10, 2008 8:00 AM

DotNetKicks.com said:

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

# March 10, 2008 10:12 AM

Christopher Bennage said:

We've had a few projects that involved custom styling for many of the standard controls. At first

# June 27, 2008 1:54 AM

Community Blogs said:

We've had a few projects that involved custom styling for many of the standard controls. At first

# June 27, 2008 2:17 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!