In this post I'll show how did I implemented the ItemsControl implicit data-template using the ImplicitItemsTemplateBehavior attached behavior.



But first, lets talk a bit about how WPF searches for an implicit data template, given a collection of items, so we can mimic that behavior in Silverlight.
Having an ItemsControl with an ItemsSource property set to a collection of type Shape, WPF looks at the ItemsControl.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 each element in the bound list (or base classes – but not interfaces!) as the key (implicitly or explicitly set).
First it looks at the ItemsControl 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 ImplicitItemTemplateBehavior attached behavior:
Code Snippet
- public class ImplicitItemTemplateBehavior : Behavior<ItemsControl>
- {
- protected override void OnAttached()
- {
- AssociatedObject.ItemTemplate = ImplicitDataTemplateResources.Instance.ImplicitDataTemplate;
-
- base.OnAttached();
- }
-
- protected override void OnDetaching()
- {
- AssociatedObject.ItemTemplate = null;
-
- base.OnDetaching();
- }
- }
Quite different than what I did with the ContentTemplateBehavior, here I'm setting the ItemsControl.ItemTemplate with a special DataTemplate I created ahead. This data-template will be picked for each item bound with the ItemsControl and it looks like this:
Code Snippet
- <DataTemplate x:Key="ImplicitDataTemplate_240C1930-91F0-4F08-A59C-20D0F9AE34C1">
- <local:ImplicitItemTemplate Content="{Binding}" />
- </DataTemplate>
The data-template above defines a special custom content-control called ImplicitItemTemplate which is bound with Item-X. This uses the same ImplicitDataTemplateResolver presented before to resolve the correct data-template for the item, in its Loaded event.
Note: I've created a regular Resource Dictionary containing the DataTemplate above, but added a C# file to it so I can load the DataTemplate quietly and safely extract the DataTemplate using unique key.
Here is the code for ImplicitItemTemplate:
Code Snippet
- public class ImplicitItemTemplate : ContentPresenter
- {
- public ImplicitItemTemplate()
- {
- Loaded += (s, e) => ContentTemplate = ImplicitDataTemplateResolver.Resolve(this);
- }
- }
Simple as that!
Now that you've an option to use the smart WPF implicit DataTemplate mechanism with Silverlight 4, you're ready for Silverlight 5 Implicit DataTemplate and still you can use this with your Silverlight 4 applications if you're not planning to move soon.
And most important, you can use my solution with Windows Phone Mango edition which supports Silverlight 4.
Feel free to download this demo code.