In this post I’ll show how did I implemented the ContentControl implicit data-template using the ImplicitContentTemplateBehavior attached behavior.
But first, lets talk a bit about how WPF searches for an implicit data template, given a content, so we can mimic that behavior in Silverlight.
Having a ContentControl with a Content set to an instance of type Circle, WPF looks at the ControlControl.ItemTemplate. In case that one is missing, and there is no ItemTemplateSelector, it changes strategy, and tries finding a Data Template by traversing upward the logical-tree, looking inside each element resource dictionary for a template which has the type of Circle (or base classes – but not interfaces!) as the key (implicitly or explicitly set).
First it looks at the ContentControl resources, if not found it goes one level up, and searches at the parent level until one is found or until reach the root element, then finishing by searching at the Application level resources.
So now that we have an idea, lets look at the ImplicitContentTemplateBehavior attached behavior.
As you can see, I’ve implemented the OnAttached method of the attached behavior by creating a Binding extension to bind the ContentControl (the associated object) Content Template property with the implicit data template, using a value converter which actually searches for the data template upward the tree.
Whenever the Content property is changing, the converter Convert method is called. Here I’m using a helper class to find the data template.
Lets look at the ImplicitDataTemplateResolver helper:
The Resolve static method, calls a Resolve private method with the new content. This calls the InternalResolve with the content full type name (I’m using this as a unique key since we can’t use x:Type in Silverlight 4), which in turn, traverse upward the Visual Tree using the VisualTreeHelper helper class. In case that a Data Template was found, it returns it. If not, keep searching up until reaching the root element. Then looks at the Application resources as last resort.
Note: For simplicity, I didn’t try to find Data Template defined for base classes, but feel free to add that if missing.
Here is the code of this sample.
In my next post, I’ll show how did I implemented the ImplicitItemTemplateBehavior attached behavior, which uses the same ImplicitDataTemplateResolver helper class but with a different approach.