WPF: Adding Metadata to objects in XAML using a MarkupExtension

A Visual Studio 2008 Solution with the complete code listing for this posting is attached.

It is quite common to augment data with metadata. An example is the set of tags on this post. How would we represent such metadata in a XAML document?

XAML provides a method for augmenting our objects - the Attached Property. It would be very simple to have a "Metadata" Attached Property whose type is a collection of metadata objects. When this attached property is set, the metadata is stored in the appropriate place and cross referenced with the data object to which the metadata refers. Simple! Well, not really.

The Attached Property method is all well and good so long as the data object is a DependencyObject because Attached Properties can only be attached to DependencyObjects. It is possible, however, to use a MarkupExtension to do a lot of what an Attached Property does.

In this article I demonstrate how to use a custom MarkupExtension to register metadata for non-DependencyObject types.

Subscribe to RSS - Metadata