title: ListBox DataTemplate for dynamically loaded type
body: <p>I have a sample MVVM WPF application and I'm having problems creating DataTemplates for my dynamically loaded model. Let me try explain:</p>
  
  <p>I have the following simplified classes as part of my Model, which I'm loading dynamically</p>
  
  <pre><code>public class Relationship
  {
      public string Category { get; set; }
      public ParticipantsType Participants { get; set; }
  }
  
  public class ParticipantsType
  {
      public ObservableCollection&lt;ParticipantType&gt; Participant { get; set; }
  }
  
  public class ParticipantType
  {
  
  }
  
  public class EmployeeParticipant : ParticipantType
  {
      public EmployeeIdentityType Employee { get; set; }
  }
  
  public class DepartmentParticipant : ParticipantType
  {
      public DepartmentIdentityType Department { get; set; }
  }
  
  public class EmployeeIdentityType
  {
      public string ID { get; set; }
  }
  
  public class DepartmentIdentityType
  {
      public string ID { get; set; }
  }
  </code></pre>
  
  <p>Here is how my View Model looks like. I created a generic object <code>Model</code> property to expose my Model:</p>
  
  <pre><code>    public class MainViewModel : ViewModelBase&lt;MainViewModel&gt;
  {
      public MainViewModel()
      {
          SetMockModel();
      }
  
      private void SetMockModel()
      {
          Relationship rel = new Relationship();
          rel.Category = "213";
          EmployeeParticipant emp = new EmployeeParticipant();
          emp.Employee = new EmployeeIdentityType();
          emp.Employee.ID = "222";
          DepartmentParticipant dep = new DepartmentParticipant();            
          dep.Department = new DepartmentIdentityType();
          dep.Department.ID = "444";
          rel.Participants = new ParticipantsType() { Participant = new ObservableCollection&lt;ParticipantType&gt;() };
          rel.Participants.Participant.Add(emp);
          rel.Participants.Participant.Add(dep);            
          Model = rel;
      }
  
      private object _Model;
      public object Model
      {
          get { return _Model; }
          set
          {
              _Model = value;
              NotifyPropertyChanged(m =&gt; m.Model);
          }
      }
  }
  </code></pre>
  
  <p>Then I tried creating a ListBox to display specifically the Participants Collection:</p>
  
  <pre><code>&lt;ListBox ItemsSource="{Binding Path=Model.Participants.Participant}"&gt;
  &lt;ListBox.ItemTemplate&gt;
      &lt;DataTemplate&gt;
          &lt;StackPanel&gt;
              &lt;Expander Header="IdentityFields"&gt;
              &lt;!-- WHAT TO PUT HERE IF PARTICIPANTS HAVE DIFFERENT PROPERTY NAMES --&gt;
              &lt;/Expander&gt;
          &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  &lt;/ListBox.ItemTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>The problem is:</p>
  
  <ol>
  <li>I don't know how to create a template that can handle both type of ParticipantTypes, in this case I could have EmployeeParticipant or DepartmentParticipant so depending on that, the data binding Path would be set to <code>Employee</code> or <code>Department</code> properties accordingly</li>
  <li>I though about creating a DataTemplate for each type (e.g. x:Type EmployeeParticipant) but the problem is that my <strong>classes in my model are loaded dynamically at runtime</strong> so VisualStudio will complain that those types don't exist in the current solution.</li>
  </ol>
  
  <p>How could I represent this data in a ListBox then if my concrete types are not known at compile time, but only at runtime? </p>
  
  <p><strong>EDIT:</strong> Added my test ViewModel class</p>