Wednesday, February 9, 2011

Control Template Named Parts in WPF

An important thing to remember and very nice to use: Control template named parts. Why and How?

Why this feature is important? because some controls that you create a ControlTemplate for, expect your ControlTemplate to have some elements in it that are necessary to the control's functionality.
How this feature is used? the templated control assumes that the template created for this control has some named elements. The templated control later, tries to access them and their functionality using the assumed names. So if you decided to create control template for such a control, you will need to add these required elements and give them the names that are expected by the templated control.

Unfortunately, most of the time named parts are not documented (Read the Update at the bottom please). How to identify them? you can use snoop, very simple: run your wpf application, run snoop, use snoop to browse your wpf application and inspect the visual tree of the desired control. The standard way to name these parts is by adding a prefix "PART_".

Or you develop your own snoop-like-app as suggested by Christopher Bennage in the following article:
Nice and short article: Identifying Control Templates Parts in WPF, by Christopher Bennage

I hope this helps

UPDATE : Microsoft now provides the names on this link.

No comments: