If you’ve done any digging into WPF control templates, you’ve likely come across an element or two called “PART_[something].”  These guys are scattered throughout the default control styles and are also widely used in SimpleStyles.  You might be wondering what it means and what it does.

So, here’s the skinny: PART_ is just a naming convention.  It means that the control expects an element with that name to exist in the template and will be looking for it.  Think of it as a way to flag that element as important and an indication that the name shouldn’t change. The control could be looking for that element for a variety of reasons.  A common example is to add an event handler.

In fact, Any time the control needs to interact directly with a “part” of its template, the “official” best practice is to use the PART_ naming convention.  The other part of that best practice, though, is that things should “fail gracefully” when that element isn’t available.  Obviously, some parts matter more than others and a missing part might mean missing functionality, but things shouldn’t blow up (exceptions, etc.).

This is important for a number of reasons, but especially because it is common for elements to be missing while a template author is mid-authoring.  This is especially true in a tool like Expression Interactive Designer.

The official manifesto on these kinds of best practices can be found here, although it looks like the updated version with the PART_ convention hasn’t made its way into the SDK (that may have been my bad).

In the meantime, if you’re authoring templates this should help to clarify things a little.  I don’t think anyone has documented the complete list of “PART_” parts, but I believe they are all represented in SimpleStyles.  Also, if you’re building controls, it would be a great idea to follow this convention whenever possible.

Leave a Reply

Your email address will not be published. Required fields are marked *