title: Binding to ItemsSource of TabControl in WPF
body: <p>I am trying to create a User Control that represents what I am calling a workspace (a reference from a blog by Josh Smith). The workspaces will be displayed in a tab control. I am aiming to use a tabbed interface to manage various documents that I have open much like in a browser of an excal work book.</p>
  
  <p>Each time a user opens a new workspace, that workspace should be displayed in the tab control. Each Workspace takes the form of a user control, and each workspace has its own view model. I would like for the Tab Header to display a property from my view model which I think will likely have to be exposed as a property through my user control.</p>
  
  <p>So far, the cleanest solution that I liked the best until I ran into numerous issues was by using datatemplates. Basically I Did the following:</p>
  
  <pre><code>&lt;DataTemplate x:Key="WorkspaceItem"&gt;
              &lt;DockPanel Width="120"&gt;
                  &lt;ContentPresenter 
                      Content="{Binding Title}" 
                      VerticalAlignment="Center" 
                      /&gt;
              &lt;/DockPanel&gt;
          &lt;/DataTemplate&gt;     
  
  &lt;DataTemplate DataType="{x:Type CustomerViewModel}"&gt;
     &lt;workspace:CustomerWorkspace /&gt;
  &lt;/DataTemplate&gt;
  
  &lt;TabControl ItemsSource="{Binding Workspaces}"
              ItemTemplate="{StaticResource WorkspaceItem}"/&gt;
  </code></pre>
  
  <p>The TabControl.ItemsSource is bound to an observablecollection(of Object) which contains all of my workspaces.</p>
  
  <p>This works great except for 2 things:</p>
  
  <ol>
  <li><p>If I open multiple customers, then I have multiple workspaces open. Because of DataTemplate Recycling, I lose state when i swap from one tab to another. So everything that is not bound will lose state.</p></li>
  <li><p>The performance of swapping between Different workspaces (that use different datatemplates) is terribly slow.</p></li>
  </ol>
  
  <p>So... I found a suggestion from another user on SO to add the user controls to the ObservableCOllection and ditch the data templates. that now solves one of the problems of losing state. however, now I am faced with 2 remaining problems:</p>
  
  <ol>
  <li>How do i set the TabItem.Header property without using a DataTemplate</li>
  <li>The speed of swapping back and forth between tabs is still slow unless they are of the same DataTemplate.</li>
  </ol>
  
  <p>I then proceeded to Actually add a TabItem to the ObservableCollection in my codebehind and Set the TabItem.Content Property to that of the user control. The speed issue was now eliminated as is the losing state issue since I have removed the use of the DataTemplates. However, I am now stuck with the issue of binding a TabItem.header to the Custome "Title" Property of my usercontrol that should be displayed in the Tab Header.</p>
  
  <p>So after this terribly long post, my questions are:</p>
  
  <ol>
  <li><p>Is there any way to use datatemplates and force them to create a new Instance for each item in the collection to prevent recycling and state loss.</p>
  
  <p>1a. Is there a better alternative than what I mentioned in the post above?</p></li>
  <li><p>is there a way to do all of this through the Xaml instead of through back end code construction of Tab Items?</p></li>
  </ol>