Home Contact

Timmy Kokke

…just sorting my bubbles…

News



Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged

Twitter












Tag Cloud


Archives

Post Categories

Image Galleries

Silverlight

Syndication:

DataTemplateSelector in Silverlight

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

Feedback

# re: DataTemplateSelector in Silverlight

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. 9/29/2009 6:11 PM | rlodina

# re: DataTemplateSelector in Silverlight

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? 9/29/2009 7:51 PM | Timmy Kokke

# re: DataTemplateSelector in Silverlight

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.


10/12/2009 7:33 AM | rlodina

# re: DataTemplateSelector in Silverlight

Thanks. I was just trying to figure out why the control template defined in my silverlight 2 app's app.xaml wasn't applying to all controls of the target type. I guess I can stop looking. Sucks that this doesn't work. 1/12/2010 1:17 PM | www.playcasinobj.com

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: