title: How do I use a ItemTemplateSelector together with a HeaderTemplate in a WPF ListView?
body: <p>Suppose the following XAML defined window:</p>
  
  <pre><code>&lt;Window x:Class="LayoutTests.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:LayoutTests"
          Title="Window1"&gt;
    &lt;Window.Resources&gt;
      &lt;XmlDataProvider x:Key="XmlData" IsInitialLoadEnabled="True"&gt;
        &lt;x:XData&gt;
          &lt;Items xmlns=""&gt;
            &lt;Item text="Item 1" type="A" /&gt;
            &lt;Item text="Item 2" type="B" /&gt;
            &lt;Item text="Item 3" type="A" /&gt;
          &lt;/Items&gt;
        &lt;/x:XData&gt;
      &lt;/XmlDataProvider&gt;
      &lt;DataTemplate x:Key="TypeATemplate"&gt;
        &lt;TextBlock Text="{Binding XPath=./@text}" Foreground="Red"/&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate x:Key="TypeBTemplate"&gt;
        &lt;TextBlock Text="{Binding XPath=./@text}" Foreground="Green"/&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate x:Key="HeaderTemplate"&gt;
        &lt;TextBlock Text="A Header"/&gt;
      &lt;/DataTemplate&gt;
      &lt;local:TypeSelector x:Key="TypeSelector" TypeATemplate="{StaticResource TypeATemplate}" TypeBTemplate="{StaticResource TypeBTemplate}"/&gt;
    &lt;/Window.Resources&gt;
      &lt;Grid&gt;
      &lt;ListView ItemsSource="{Binding Source={StaticResource XmlData}, XPath='/Items/Item'}" ItemTemplateSelector="{StaticResource TypeSelector}"&gt;
        &lt;!--&lt;ListView.View&gt;
          &lt;GridView&gt;
            &lt;GridViewColumn Width="Auto" HeaderTemplate="{StaticResource HeaderTemplate}"/&gt;
          &lt;/GridView&gt;
        &lt;/ListView.View&gt;--&gt;
      &lt;/ListView&gt;
    &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And a <code>DataTemplateSelector</code> defined in the code behind like this:</p>
  
  <pre><code>  public class TypeSelector : DataTemplateSelector
    {
      public DataTemplate TypeATemplate { get; set; }
      public DataTemplate TypeBTemplate { get; set; }
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
        var element = item as XmlElement;
        if (element.Attributes["type"].Value == "A")
          return TypeATemplate;
        else
          return TypeBTemplate;
      }
    }
  </code></pre>
  
  <p>If the code for the header template is not commented out, the DataTypeSelector is not called anymore. <strong>How do I specify a <code>DataTemplateSelector</code> and <code>HeaderTemplate</code> at the same time?</strong></p>