Can I use different templates for different types of items in a ListBox?

Match the list in Silverlight, similar to the facebook feed. Each element can be 1) an update of the state with the image on the left, 2) thumbnails of the photos with the title, 3) a video from YouTube, or 4) a blog entry. Each element uses a different template. How would you do that?

I planned that all my element types implement a common interface. That would mean that I would go to the IFeedItem list.

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 foresaw that the ListBox is "smart enough" to select a template based on the type of item.

+4
source share

Source: https://habr.com/ru/post/1310605/


All Articles