title: WPF ContentTemplateSelector weirdness
body: <p><strong>Goal:</strong> I wish to have an object that contains my data and have it bound to a ContentPresenter that uses a custom ContentTemplateSelector to select the proper data template for rendering in the main control.</p>
  
  <p><strong>Problem:</strong> DataTemplate does not render as expected in the main control; It does not render the specified View control.</p>
  
  <p><strong>Code:</strong> </p>
  
  <p>MainView.xaml:</p>
  
  <pre><code>&lt;Window x:Class="WpfContentTemplateSelector.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:this="clr-namespace:WpfContentTemplateSelector"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Grid.Resources&gt;
              &lt;this:ObjectWrapper x:Key="wrapper"/&gt;
          &lt;/Grid.Resources&gt;
  
          &lt;ContentPresenter Content="{Binding Value}" DataContext="{StaticResource ResourceKey=wrapper}"&gt;
              &lt;ContentPresenter.ContentTemplateSelector&gt;
                  &lt;this:TemplateSelector/&gt;
              &lt;/ContentPresenter.ContentTemplateSelector&gt;
          &lt;/ContentPresenter&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>DataObject.cs:</p>
  
  <pre><code>class DataObject
  {
      public string Title { get; set; }
  
      public DataObject()
      {
          Title = "Title";
      }
  }
  </code></pre>
  
  <p>ObjectWrapper.cs:</p>
  
  <pre><code>class ObjectWrapper
  {
      public DataObject Value { get; set; }
  
      public ObjectWrapper()
      {
          Value = new DataObject();
      }
  }
  </code></pre>
  
  <p>TemplateSelector.cs:</p>
  
  <pre><code>class TemplateSelector : DataTemplateSelector
  {
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
          DataTemplate template = new DataTemplate(typeof(View));
  
          return template;
      }
  }
  </code></pre>
  
  <p>View.xaml:</p>
  
  <pre><code>&lt;UserControl x:Class="WpfContentTemplateSelector.View"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:this="clr-namespace:WpfContentTemplateSelector"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300" Background="Navy"&gt;
      &lt;Grid Background="Navy"&gt;
          &lt;Button Height="30" Content="{Binding Title}"/&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>Summary:</strong></p>
  
  <ul>
  <li>The ObjectWrapper initializes.</li>
  <li>The DataObject initializes.</li>
  <li>On initialization, the TemplateSelector creates a new DataTemplate.</li>
  <li>The created DataTemplate uses the View type in its constructor.</li>
  <li>The objects are all initialized, the datacontext of the main view is set to the DataObject, and the view still doesn't render to the main window.</li>
  </ul>