In this post, I'll show how I implemented the ItemsControl
implicit data-template using the ImplicitItemsTemplateBehavior
attached behavior.
But first, let's 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 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 it reaches the root element, then finishes by searching at the Application level resources.
So now that we have an idea, let's look at the ImplicitItemTemplateBehavior
attached behavior:
1.public class ImplicitItemTemplateBehavior : Behavior<itemscontrol />
2.{
3. protected override void OnAttached()
4. {
5. AssociatedObject.ItemTemplate =
ImplicitDataTemplateResources.Instance.ImplicitDataTemplate;
6.
7. base.OnAttached();
8. }
9.
10. protected override void OnDetaching()
11. {
12. AssociatedObject.ItemTemplate = null;
13.
14. base.OnDetaching();
15. }
16.}
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:
1.<DataTemplate x:Key="ImplicitDataTemplate_240C1930-91F0-4F08-A59C-20D0F9AE34C1">
2. <local:ImplicitItemTemplate Content="{Binding}" />
3.</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
:
1.public class ImplicitItemTemplate : ContentPresenter
2.{
3. public ImplicitItemTemplate()
4. {
5. Loaded += (s, e) => ContentTemplate = ImplicitDataTemplateResolver.Resolve(this);
6. }
7.}
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.
Tomer Shamam is a Software Architect and a UI Expert at CodeValue, the home of software experts, based in Israel (http://codevalue.net). Tomer is a speaker in Microsoft conferences and user groups, and in-house courses. Tomer has years of experience in software development, he holds a B.A degree in computer science, and his writings appear regularly in the Israeli MSDN Pulse, and other popular developer web sites such as CodePlex and The Code Project. About his thoughts and ideas you can read in his blog (http://blogs.microsoft.co.il/blogs/tomershamam).