title: Dynamic Tabitem with Predefined Template
body: <p>I have a WPF app which creates tab dynamically,now I want the <strong>every tab item must contain expander and Usercontrol</strong> and also ill <strong>set the datacontext for Expander and Usercontrol during run time</strong>, so if i define the template for tabitem in Style,i feel it wuld be difficult for setting datacontext for expnder and usercontrol. In order for HeaderTemplate</p>
  
  <p>XAML Code</p>
  
  <pre><code>&lt;TabControl Name="tabDynamic" ItemsSource="{Binding}" SelectionChanged="tabDynamic_SelectionChanged" FontSize="15" FontFamily="Verdana"  FontWeight="Normal"    FontStretch="Expanded" &gt;
              &lt;TabControl.Resources&gt;
                  &lt;DataTemplate x:Key="TabHeader" DataType="TabItem"&gt;
                      &lt;DockPanel&gt;
                          &lt;Button Name="btnDelete" DockPanel.Dock="Right" Margin="150,0,0,0" Content="X" Foreground="WhiteSmoke"  FontSize="10" 
                                 FontWeight="Bold"   Padding="0" Click="btnDelete_Click" Height="15"
                                 CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}"   &gt;
                              &lt;Button.Background&gt;
                                  &lt;LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"&gt;
                                      &lt;GradientStop Color="#FF9D4040" Offset="0" /&gt;
                                      &lt;GradientStop Color="#FFB11212" Offset="1" /&gt;
                                  &lt;/LinearGradientBrush&gt;
                              &lt;/Button.Background&gt;
                              &lt;!--&lt;Image Source="/delete.gif" Height="11" Width="11"&gt;&lt;/Image&gt;--&gt;
                          &lt;/Button&gt;
                          &lt;TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem }, Path=Header}"  /&gt;
                      &lt;/DockPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TabControl.Resources&gt;
          &lt;/TabControl&gt;
  </code></pre>
  
  <p><strong>In codebehind ill add the content,</strong></p>
  
  <pre><code>     Private Function AddTabItem() As TabItem
          Dim count As Integer = _tabItems.Count
  
          Dim tab As New TabItem()
  
          tab.Header = String.Format("tab{0}", count)
          tab.Name = String.Format("tab{0}", count)
          tab.HeaderTemplate = TryCast(tabDynamic.FindResource("TabHeader"), DataTemplate)
  
          _tabItems.Insert(count - 1, tab)
  
            obj = New ThumbnailImages
  
          ' add controls to tab item, 
          If ImageCollection.Count &gt; 0 Then
              obj.SetDataContext(ImageCollection)
              tab.Content = obj
          End If
          Return tab
      End Function
  </code></pre>
  
  <p>Now i need the <strong>content to be templated with expander and Usercontrol</strong>, how can i achieve ?</p>