title: Listbox ItemTemplate Selector does not pick a template
body: <p>I am trying to use an ItemTemplateSelector on a listbox within a grid that I am creating on a different file to later be called by the MainWindow. </p>
  
  <p>Here is my DataTemplateSelector code</p>
  
  <pre><code> public class TemplateSelector : DataTemplateSelector
  {
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
          FrameworkElement element = container as FrameworkElement;
          if (element != null &amp;&amp; item != null &amp;&amp; item is myClass)
          {
              myClass agg = item as myClass;
              if(agg.myType == a)
              {
                  return element.FindResource("greenItemTemplate") as DataTemplate;
              }
              else if (agg.myType == b)
              {
                  return element.FindResource("yellowItemTemplate") as DataTemplate;
              }
              else if (agg.myType == c)
              {
                  return element.FindResource("redItemTemplate") as DataTemplate;
              }
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p>Here is my xaml</p>
  
  <pre><code> &lt;Grid x:Class="NS.Views.ListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:NS.Commons"
    DataContext="{Binding}"&gt;
  &lt;Grid.Resources&gt;
      &lt;c:myConverter x:Key="converter" /&gt;
      &lt;c:TemplateSelector x:Key="borderSelector" /&gt;
      &lt;DataTemplate x:Key="greenItemTemplate"&gt;
          &lt;Border BorderThickness="3" BorderBrush="Green"&gt;
              &lt;StackPanel HorizontalAlignment="Stretch"&gt;
                  &lt;TextBlock Text="{Binding Path=GroupName}"/&gt;
                  &lt;TextBlock Text="{Binding Path=myType}"/&gt;
              &lt;/StackPanel&gt;
          &lt;/Border&gt;
      &lt;/DataTemplate&gt;
             &lt;DataTemplate x:Key="redItemTemplate"&gt;
          &lt;Border BorderThickness="3" BorderBrush="Red"&gt;
              &lt;StackPanel HorizontalAlignment="Stretch"&gt;
                  &lt;TextBlock Text="{Binding Path=GroupName}"/&gt;
                  &lt;TextBlock Text="{Binding Path=myType}"/&gt;
              &lt;/StackPanel&gt;
          &lt;/Border&gt;
      &lt;/DataTemplate&gt;
     &lt;DataTemplate x:Key="yellowItemTemplate"&gt;
          &lt;Border BorderThickness="3" BorderBrush="Yellow"&gt;
              &lt;StackPanel HorizontalAlignment="Stretch"&gt;
                  &lt;TextBlock Text="{Binding Path=GroupName}"/&gt;
                  &lt;TextBlock Text="{Binding Path=myType}"/&gt;
              &lt;/StackPanel&gt;
          &lt;/Border&gt;
      &lt;/DataTemplate&gt;
  &lt;/Grid.Resources&gt;
  
  &lt;ListBox ItemsSource="{Binding myCollectionOfMyClassObjects}" 
           Name="listBox1"
           HorizontalContentAlignment="Stretch"  
           ItemTemplateSelector="{StaticResource borderSelector}"
           &gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p></p>
  
  <p>However, although the binding works fine (the list of non-formatted objects appears in the list), the ItemTemplateSelector is not calling the TemplateSelector methods. I put a breakpoint at the beginning of the methods and it's never called.</p>
  
  <p>Does anyone knows what could be the problem?</p>