title: Selecting DataTemplate based on sub-object type
body: <p>I want to databind an ItemsCollection, but instead of rendering the collection items, I want to render sub-objects reached via a property on the collection item.</p>
  
  <p>To be more specific: this will be a 2D map viewer for a game (though in its current state it isn't 2D yet). I databind an ItemsControl to an ObservableCollection&lt;Square&gt;, where Square has a property called Terrain (of type Terrain). Terrain is a base class and has various descendants.</p>
  
  <p>What I want is for the ItemsControl to render the Terrain property from each collection element, not the collection element itself.</p>
  
  <p>I can already make this work, but with some unnecessary overhead. I want to know if there's a good way to remove the unnecessary overhead.</p>
  
  <p>What I currently have are the following classes (simplified):</p>
  
  <pre><code>public class Terrain {}
  public class Dirt : Terrain {}
  public class SteelPlate : Terrain {}
  public class Square
  {
      public Square(Terrain terrain)
      {
          Terrain = terrain;
      }
      public Terrain Terrain { get; private set; }
      // additional properties not relevant here
  }
  </code></pre>
  
  <p>And a UserControl called MapView, containing the following:</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;DataTemplate DataType="{x:Type TerrainDataModels:Square}"&gt;
          &lt;ContentControl Content="{Binding Path=Terrain}"/&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type TerrainDataModels:Dirt}"&gt;
          &lt;Canvas Width="40" Height="40" Background="Tan"/&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type TerrainDataModels:SteelPlate}"&gt;
          &lt;Canvas Width="40" Height="40" Background="Silver"/&gt;
      &lt;/DataTemplate&gt;
  &lt;/UserControl.Resources&gt;
  &lt;ItemsControl ItemsSource="{Binding}"/&gt;
  </code></pre>
  
  <p>Given this code, if I do:</p>
  
  <pre><code>mapView.DataContext = new ObservableCollection&lt;Square&gt; {
      new Square(new Dirt()),
      new Square(new SteelPlate())
  };
  </code></pre>
  
  <p>I get something that looks exactly like what I expect: a StackPanel containing a tan box (for the Dirt) and a silver box (for the SteelPlate). But I get it with unnecessary overhead.</p>
  
  <p>My specific concern is with my DataTemplate for Square:</p>
  
  <pre><code>&lt;DataTemplate DataType="{x:Type TerrainDataModels:Square}"&gt;
      &lt;ContentControl Content="{Binding Path=Terrain}"/&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>What I really want to say is "no, don't bother rendering the Square itself, render its Terrain property instead". This gets close to that, but this adds an extra two controls to the visual tree for every Square: a ContentControl, as coded explicitly in the above XAML, and its ContentPresenter. I don't particularly want a ContentControl here; I really want to short-circuit and insert the Terrain property's DataTemplate directly into the control tree.</p>
  
  <p>But how do I tell the ItemsControl to render collectionitem.Terrain (thus looking up one of the above DataTemplates for the Terrain object) rather than rendering collectionitem (and looking for a DataTemplate for the Square object)?</p>
  
  <p>I want to use DataTemplates for the terrains, but not at all necessarily for the Square -- that was just the first approach I found that worked adequately. In fact, what I really want to do is something completely different -- I really want to set the ItemsControl's DisplayMemberPath to "Terrain". That renders the right object (the Dirt or SteelPlate object) directly, without adding an extra ContentControl or ContentPresenter. Unfortunately, DisplayMemberPath always renders a string, ignoring the DataTemplates for the terrains. So it's got the right idea, but it's useless to me.</p>
  
  <p>This whole thing may be premature optimization, and if there's no <em>easy</em> way to get what I want, I'll live with what I've got. But if there's a "WPF way" I don't yet know about to bind to a property instead of the whole collection item, it'll add to my understanding of WPF, which is really what I'm after.</p>