title: WPF Custom DataTemplateSelector based on Enum property in Custom UserControl
body: <p>I've just learnt about creating custom UserControls and I am now learning about DataTemplates, still learning about the properties though so I can imagine that the implementation of the control property is wrong.</p>
  
  <p>I've had a go at creating a UserControl called <code>ExtendedListBox</code> which can display its items with a checkbox or a normal listbox. Below is the implementation of the <code>ExtendedListBox</code> The property <code>DisplayAs</code> is used for picking which DataTemplate to use.</p>
  
  <p>I've had a look at quite a few questions relating to DataTemplateSelectors here on StackOverflow and it now has me questioning this implementation, should this be done by ContentControl? (See: <a href="https://stackoverflow.com/questions/22011005/bind-a-property-to-datatemplateselector">Bind a property to DataTemplateSelector</a>)</p>
  
  <pre><code>public partial class ExtendedListBox : UserControl
  {
      public ExtendedListBoxDisplay DisplayAs { get; set; }
  
      public ExtendedListBox()
      {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p>Here is the xaml for the user control the DataTemplates are in the UserControl Resources as CheckedListBoxTemplate and ListBoxTemplate</p>
  
  <pre><code>&lt;UserControl x:Class="UserControlTest.Configuration.Controls.ExtendedListBox"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:enums="clr-UserControlTest.Configuration.Enums"
               xmlns:local="clr-UserControlTest.Configuration.Controls"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:templateSelectors="clr-namespace:UserControlTest.Configuration.Controls.TemplateSelectors"
               x:Name="UcExtendedListBox"
               d:DesignHeight="84"
               d:DesignWidth="100"
               SizeChanged="ResizeControl"
               mc:Ignorable="d"&gt;
      &lt;UserControl.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;DataTemplate x:Key="CheckedListBoxTemplate" DataType="enums:ExtendedListBoxDisplay"&gt;
                  &lt;CheckBox IsChecked="{Binding IsChecked}"&gt;
                      &lt;TextBlock Text="{Binding}" TextWrapping="Wrap" /&gt;
                  &lt;/CheckBox&gt;
              &lt;/DataTemplate&gt;
              &lt;DataTemplate x:Key="ListBoxTemplate" DataType="enums:ExtendedListBoxDisplay"&gt;
                  &lt;TextBlock Text="{Binding}" TextWrapping="Wrap" /&gt;
              &lt;/DataTemplate&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/UserControl.Resources&gt;
      &lt;ListBox x:Name="LstItems"
               Grid.Row="2"
               Width="{Binding ActualWidth,
                               ElementName=UcExtendedListBox}"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               HorizontalContentAlignment="Center"
               ScrollViewer.HorizontalScrollBarVisibility="Disabled"
               ScrollViewer.VerticalScrollBarVisibility="Visible"
               SelectionChanged="RaiseSelectionChanged"&gt;
                      &lt;ItemsControl&gt;
              &lt;ItemsControl.ItemTemplateSelector&gt;
                  &lt;templateSelectors:ExtendedListBoxDataTemplateSelector 
                          CheckedListBoxTemplate="{StaticResource CheckedListBoxTemplate}"
                          ListBoxTemplate="{StaticResource ListBoxTemplate}" /&gt;
              &lt;/ItemsControl.ItemTemplateSelector&gt;
          &lt;/ItemsControl&gt;
      &lt;/ListBox&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>This is the DataTemplateSelector its implementation is rather simple.</p>
  
  <pre><code>public class ExtendedListBoxDataTemplateSelector : DataTemplateSelector
  {
      public DataTemplate CheckedListBoxTemplate { get; set; }
  
      public DataTemplate ListBoxTemplate { get; set; }
  
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
          if (item == null)
          {
              return base.SelectTemplate(item, container);
          }
  
          var data = (ExtendedListBoxDisplay)item;
  
          switch (data)
          {
              case ExtendedListBoxDisplay.CheckedListBox:
                  return CheckedListBoxTemplate;
              case ExtendedListBoxDisplay.ListBox:
                  return ListBoxTemplate;
              default:
                  return base.SelectTemplate(item, container);
  
          }
      }
  }
  </code></pre>
  
  <p>So I've gone to run this and it doesn't work, I'm guessing that its because I haven't binded the items and the property to look for? Would any please be able to guide me in the correct direction as a first time user of DataTemplateSelectors? </p>
  
  <p>Many thanks.</p>