How to show a collection of objects of different (inherited) types

Topics: Questions
Jul 20, 2012 at 1:16 PM
Edited Jul 20, 2012 at 1:21 PM


I have a class [Person] which contains a collection<Role>.
The [Role] class is abstract and has multiple implementations.

The following code is very simplificated.
There are more Role implmementations, and they have more different properties. 


public class Person
  public string Name {get; set;}
  public Collection<Role> Roles {get; set;}

public abstract class Role
  public string RoleName {get; set;}

public class CustomerRole : Role
  public string CustomerNumber {get; set;}
  public DateTime CustomerSince {get; set;

public class SupplierRole : Role
  public string SupplierNumber {get; set;}
  public string OurCustomerNumber {get; set;}

My goal is to define an individual UserControl for each Role implementation.
So I have a [CustomerRoleView] with a [CustomerViewModel] and so on.

Now the problem: How can I display a list of the persons roles within [PersonView] ?
The list has to instanciate the correct view based on the concrete type of the current item.

Thanks, Alex.
Jul 23, 2012 at 5:31 PM

Try to use a datatemplateselector.