views:

28

answers:

1

Picture a listbox in Silverlight that is similar to the "feed" on facebook. Each item might be 1) a status update with an image on the left, 2) a photo thumbnail with a title, 3) a youtube video, or 4) a blog entry. Each item uses a different template. How would you accomplish this?

I had planned on all my item types implement a common interface. This would mean I would pass in a list of IFeedItem.

public interface IFeedItem
{
   DateTime Published { get; set; }
   string Owner { get; set; }
   string SourceUrl { get; set; }
}

public class StatusUpdateFeedItem : IFeedItem
{
   DateTime Published { get; set; }
   string Owner { get; set; }
   string SourceUrl { get; set; }
   ... more
}

public class PhotoFeedItem : IFeedItem
{
   DateTime Published { get; set; }
   string Owner { get; set; }
   string SourceUrl { get; set; }
   ... more
}

public class VideoFeedItem : IFeedItem
{
   DateTime Published { get; set; }
   string Owner { get; set; }
   string SourceUrl { get; set; }
   ... more
}

public class BlogEntryFeedItem : IFeedItem
{
   DateTime Published { get; set; }
   string Owner { get; set; }
   string SourceUrl { get; set; }
   ... more
}

//build the list
var list = new List<IFeedItem>
{
    new StatusUpdateFeedItem(),
    new PhotoFeedItem(),
    new VideoFeedItem(),
    new BlogEntryFeedItem()
}

ListBox1.ItemsSource = list;

After that, I envisioned the ListBox being "smart enough" somehow to choose a template based on item type.

A: 

I found a great solution to this problem at http://geekswithblogs.net/tkokke/archive/2009/09/28/datatemplateselector-in-silverlight.aspx.

Byron Sommardahl

related questions