title: WPF - Databind to a StackPanel using DataTemplates
body: <p>I've modified my question since it has changed focus when trying things out.
  I narrowed the problem down to the following...</p>
  
  <p>I try to bind the selected Item of a TreeView to a StackPanel (or some other container that can hold User Controls). This container will then display a UserControl, depending on the type of the selected item.</p>
  
  <p>Here is the xaml of the StackPanel (both treeview and stackpanel are in the same window ==> different grid column)</p>
  
  <pre><code>&lt;StackPanel Grid.Column="2" MinWidth="500" DataContext="{Binding ElementName=myTree, Path=SelectedItem, Mode=OneWay}"&gt;
      &lt;StackPanel.Resources&gt;
          &lt;DataTemplate DataType="{x:Type mvTypes:MyTypeA}"&gt;
              &lt;controls:UserControlA DataContext="{Binding}" /&gt;
          &lt;/DataTemplate&gt;
          &lt;DataTemplate DataType="{x:Type mvTypes:MyTypeB}"&gt;
              &lt;controls:UserControlB DataContext="{Binding}" /&gt;
          &lt;/DataTemplate&gt;
      &lt;/StackPanel.Resources&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>When I place a user control directly under the stackpanel (not in the resources), it displays it with the selected object as their datacontext.
  Idem if I place a TextBox in it, it will show the correct type of the selected item.</p>
  
  <pre><code>&lt;TextBox Name="textBox1" Text="{Binding}" /&gt;
  </code></pre>
  
  <p>For some reason, placing it within a DataTemplate (even without setting the DataType) results in nothing to display. </p>
  
  <p>Any sugestions. I'm thinking that maybe a StackPanel is not the right control for this, though I can't seem to find other controls that look suitable as containers like this.</p>
  
  <p>Thanks in advance.</p>