title: Databind WPF TreeView With Mixed Types and Categorized Sub-TreeViewItems
body: <p>I'm trying to build a TreeView in WPF that has categories.  </p>
  
  <p>Basically the structure looks like this:</p>
  
  <pre><code>Root  
   |_
   | Cat A
   |_
   | Cat B
   |_
     Cat C
  </code></pre>
  
  <p>Each Category is bound to a separate observable collection.  Most of my categories are very simple.  The TreeViewItem that is the category has it's ItemsSource set and there is no hierarchy.  However, I do have a category which needs to be represented in a special hierarchy.</p>
  
  <p>Image that "Cat C" in the above tree then becomes like this:</p>
  
  <pre><code>Servers
    |_
    | [SERVER A's DISPLAY NAME]
    |  |_
    |  | Namespaces
    |  |  |_
    |  |_   [NAMESPACE alpha's DISPLAY NAME]
    |  | Deployments
    |  |  |_
    |  |_   [DEPLOYMENT 1's DISPLAY NAME]
    |_   Configuration File
      [SERVER B's DISPLAY NAME]
  </code></pre>
  
  <p>Basically, what I want is the hard coded parent TreeViewItem to have a static header.  That item should then have a collection of items with their names shown.  For each child of the original parent there should be three static items which have a dynamic list of children.</p>
  
  <p>Writing this out makes me think that this should be a very simplistic problem to solve.  However, after playing with the XAML for a couple of days I cannot get a hierarchy to work.  Below is the farthest I've been able to get.  I've used a composite collection called Children to get the Namespaces, Deployments, and the Configuration File into one collection.  However, I cannot separate them out.</p>
  
  <pre><code>&lt;TreeViewItem ItemsSource="{Binding Path=Configuration.Servers}" 
                        IsExpanded="True" &gt;
              &lt;TreeViewItem.ItemContainerStyle&gt;
                  &lt;Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}"&gt;
                      &lt;Setter Property="IsExpanded" Value="True"/&gt;
                  &lt;/Style&gt;
              &lt;/TreeViewItem.ItemContainerStyle&gt;
  
              &lt;TreeViewItem.HeaderTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Border Margin="0,2,2,0"&gt;
                          &lt;StackPanel Orientation="Horizontal"&gt;
                              &lt;Image Source="/WPF;component/Images/server_chart.png"
                                     Margin="0,0,5,0"/&gt;
                              &lt;TextBlock Text="Cognos Servers" /&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Border&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TreeViewItem.HeaderTemplate&gt;
  
              &lt;TreeViewItem.Resources&gt;
                  &lt;HierarchicalDataTemplate ItemSource="{Binding Path=Children}" DataType="{x:Type local:Server}"&gt;
                      &lt;HierarchicalDataTemplate.ItemContainerStyle&gt;
                          &lt;Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}" &gt;
                              &lt;Setter Property="IsExpanded" Value="True"/&gt;
                          &lt;/Style&gt;
                      &lt;/HierarchicalDataTemplate.ItemContainerStyle&gt;
                      &lt;TextBlock Text="{Binding DisplayName}" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown"&gt;
                          &lt;TextBlock.ContextMenu&gt;
                              &lt;ContextMenu&gt;
                                  &lt;MenuItem Header="Refresh" Click="TreeItemMenu_AddNewClient"&gt;
                                      &lt;MenuItem.Icon&gt;
                                          &lt;Image Source="/WPF;component/Images/arrow_refresh.png" /&gt;
                                      &lt;/MenuItem.Icon&gt;
                                  &lt;/MenuItem&gt;
                                  &lt;MenuItem Header="Add Client..." Click="TreeItemMenu_AddNewClient" /&gt;
                                  &lt;Separator /&gt;
                                  &lt;MenuItem Header="Remove" Click="TreeItemMenu_RemoveClick"&gt;
                                      &lt;MenuItem.Icon&gt;
                                          &lt;Image Source="/WPF;component/Images/server_delete.png" /&gt;
                                      &lt;/MenuItem.Icon&gt;
                                  &lt;/MenuItem&gt;
                              &lt;/ContextMenu&gt;
                          &lt;/TextBlock.ContextMenu&gt;
                      &lt;/TextBlock&gt;
                  &lt;/HierarchicalDataTemplate&gt;
  
                  &lt;HierarchicalDataTemplate ItemsSource="{Binding Clients}" DataType="{x:Type local:Namespace}"&gt;
                      &lt;HierarchicalDataTemplate.ItemContainerStyle&gt;
                          &lt;Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}" &gt;
                              &lt;Setter Property="IsExpanded" Value="True"/&gt;
                          &lt;/Style&gt;
                      &lt;/HierarchicalDataTemplate.ItemContainerStyle&gt;
                      &lt;TextBlock Text="{Binding DisplayName}" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" /&gt;
                  &lt;/HierarchicalDataTemplate&gt;
  
                  &lt;DataTemplate DataType="{x:Type local:Client}"&gt;
                      &lt;TextBlock Text="{Binding DisplayName}"
                                 ContextMenu="{StaticResource ResourceKey=ContextMenuTreeItem}" 
                                 PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TreeViewItem.Resources&gt;
  </code></pre>