Silverlight Feeds - All your Silverlight feeds in one place.

Sponsors

Monday, September 28, 2009

DataTemplateSelector in Silverlight

by Timmy Kokke via Timmy Kokke on 9/28/2009 1:53:53 PM

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


email it!bookmark it!digg it!

Original Post: DataTemplateSelector in Silverlight

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us