WPF: How do I get different templates based on a list-item's type?
I heard that I could create a collection of mixed types and have a d开发者_如何学编程ifferent Data Template for each type. How woudl I do that for a ListBox?
ItemTemplateSelector
property of ListBox
is made specifically for that.
And you need a class inheriting from DataTemplateSelector and then override the SelectTemplate method:
public class SomeTemplateSelector:DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if(((YourDataContextClass)item).SomeLogic)
return FirstTemplate;
else
return OtherTemplate;
}
}
精彩评论