Geeks With Blogs

@Sorskoot
  • Sorskoot "Visual Studio 2013, will be available later this year, with a preview build publicly available at Build" http://t.co/SV28646Qr6 about 513 days ago

News


Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged
Timmy Kokke …just sorting my bubbles…

Unlike WPF, Silverlight doesn't contain a DataTemplateSelector. A DataTemplateSelector is used to select a data template based on the data-bound element and the data object. But, it isn't hard to build your own.

Start by adding a class to the solution and call it something like “DataTemplateSelector”. Inherit this class  from System.Windows.Controls.ContentControl. The ContentControl class has a property for a data template and a property for content, which you can use to data-bind to. Next, create an override on the OnContentChanged method like this:

protected override void OnContentChanged(object oldContent, object newContent) 
{

}

In the OnContentChanged method, set the template of the  to a template picked from the dictionary. Add something like the code below to method:

if (((int)newContent % 2) == 0)
{
  ContentTemplate = DataTemplateHelper.LoadFromDictionary(
                        "DataTemplateDemo;component/DataTemplates.xaml",
                        "EvenDataTemplate");
}
else
{
  ContentTemplate = DataTemplateHelper.LoadFromDictionary(
                        "DataTemplateDemo;component/DataTemplates.xaml",
                        "OddDataTemplate");
}

Personally, I prefer to place templates in a separate dictionary, just in case I need to share them between xaml files or to keep the other xaml files clean. I use a custom helper class to make this a bit easier. I’ll explain this in a bit.

In your xaml file, use the template selector class as the template of the list:

<ListBox x:Name="ListofNumbers">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <DataTemplateDemo:DataTemplateSelector 
           Content="{Binding}"/>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

 

I use this helper method like below to retrieve templates from dictionaries. To use the code, add a reference too System.Xml.Linq.

public static DataTemplate LoadFromDictionary(string dictionary,
                                              string template)
{
  var doc = XDocument.Load(dictionary);
  var dict = (ResourceDictionary)XamlReader
                    .Load(doc.ToString(SaveOptions.None));
  return dict[template] as DataTemplate;
}

 

A demo project can be downloaded here.



 

Shout it
Tags van Technorati:
dotNed blogger
kick it on DotNetKicks.com
Posted on Monday, September 28, 2009 3:53 PM Silverlight , dotnetmag , CodeProject | Back to top


Comments on this post: DataTemplateSelector in Silverlight

# re: DataTemplateSelector in Silverlight
Requesting Gravatar...
Hi.

I have problem running the demo.

The listbox have a strage behavior (only red items draw correct ). I suspect a bug in ListBox: she don't like to change ItemTemplate at run-time.

Or maybe I'm missing somethings.

Thank's for code sharing.
Left by rlodina on Sep 29, 2009 6:11 PM

# re: DataTemplateSelector in Silverlight
Requesting Gravatar...
The demo should work with Silverlight 3. Have you got it working yet? If you still have trouble, would you be so kind to send me your version of the code, to mental_c(at)hotmail(dot)com?
Left by Timmy Kokke on Sep 29, 2009 7:51 PM

# re: DataTemplateSelector in Silverlight
Requesting Gravatar...
Hi,

I send, to your hotmail account, a screen record (your sample code - unchanged) and a small sample about changing ListBox.ItemTemplate at runtime.

Thank you - and a nice day.


Left by rlodina on Oct 12, 2009 7:33 AM

# re: DataTemplateSelector in Silverlight
Requesting Gravatar...
Hi,

How can I use a data template that has a user control inside when I use a new resource dictionary file for my templates. I'm having an error saying that the namespace of my user control is unknown.

Thanks in advance.
Left by Lance on Jun 17, 2011 9:22 AM

# re: DataTemplateSelector in Silverlight
Requesting Gravatar...
Thanks Sorskoot for the article. If you read this comment (which is unlikely), could you please answer my question.

what can i do retrieve the templates not from dictionaries, but from the page resources. The problem i'm facing is how to get the instance of the page from iether of DataTemplateSelector or DataTemplateHelper.

-----

public static DataTemplate LoadFromPageResources(string dictionary,
string template)
{
???????
}

Left by Andrey on Oct 19, 2011 12:00 PM

Your comment:
 (will show your gravatar)
 


Copyright © Timmy Kokke | Powered by: GeeksWithBlogs.net | Join free