title: DataTemplateSelector for UserControl
body: <p>I have a Listbox, where I want to load in different types of UserControls in a UWP app.</p>
  
  <p>The reason I need UserControls is because there is code-behind needed for every item that will be shown in the list, so a simple DataTemplate doesn't cut it.</p>
  
  <p>I'm getting an error on the line </p>
  
  <pre><code>&lt;converters:HomeWidgetControlPicker&gt;
  </code></pre>
  
  <p>in the listbox.</p>
  
  <pre><code>The specified value cannot be assigned. The following type was expected: "DependencyObject".
  </code></pre>
  
  <p>How can I make the UserControl load? I used this method in an older UWP app and there it worked, but in my new project it doesn't.</p>
  
  <p>I cannot use x:DataType as all items that will be of the same class (HomeWidget), but depending on the Type property of HomeWidget the correct UserControl is selected.</p>
  
  <p>I made a DataTemplateSelector:</p>
  
  <pre><code>public class HomeWidgetControlPicker : DataTemplateSelector
  {
      public DataTemplate Artist { get; set; }
      public DataTemplate Release { get; set; }
  
      protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
      {
          if (item != null)
          {
              var result = (HomeWidget)item;
  
              if (result.Type == "artist")
              {
                  return Artist;
              }
  
              if (result.Type == "release")
              {
                  return Release;
              }
          }
          return null;
      }
  }
  </code></pre>
  
  <p>and I have a Listbox, currently only type (HomeWidgetControlPicker.Artist) is used, but more will follow.</p>
  
  <pre><code>        &lt;ListBox x:Name="wrapGrid" ItemContainerStyle="{StaticResource WrapPanelBorderedItem}" ItemsSource="{x:Bind WidgetList}" Background="{x:Null}" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalAlignment="Top" HorizontalAlignment="Center" &gt;
  
              &lt;ListBox.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;userControls:WrapPanel/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ListBox.ItemsPanel&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;converters:HomeWidgetControlPicker&gt;
                          &lt;converters:HomeWidgetControlPicker.Artist&gt;
                              &lt;DataTemplate&gt;
                                  &lt;homeItems:HomeArtist&gt;&lt;/homeItems:HomeArtist&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/converters:HomeWidgetControlPicker.Artist&gt;
                      &lt;/converters:HomeWidgetControlPicker&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
  </code></pre>