title: Adding TabItems dynamically
body: <p>I have a <code>TabControl</code> control </p>
  
  <pre><code>&lt;TabControl Name="Farms_myVillages"
              ItemsSource="{Binding Villages}"&gt;
  &lt;/TabControl/&gt;
  </code></pre>
  
  <p>In the code behind I add some tabs dynamically to the <code>TabControl</code> as follows:</p>
  
  <pre><code>foreach (Village vill in Villages)
  {
      TabItem tab = new TabItem();
      tab.Header = vill.Name;
      VillageUserControl c = new VillageUserControl();
      c.DataContext = vill;
      tab.Content = c;
      Farms_myVillages.Items.Add(tab);
  }
  </code></pre>
  
  <p>where <code>VillageUserControl</code> is a <code>UserControl</code> that deal with the specified village. This code works fine and it gets the expected results...</p>
  
  <p>The problem is that I don't want this to be in the code behind but just in the <code>xaml</code> itself.</p>
  
  <p>I try this:</p>
  
  <pre><code>&lt;TabControl Name="Farms_myVillages"
              ItemsSource="{Binding Villages}"&gt;
        &lt;TabControl.ItemContainerStyle&gt;
            &lt;Style TargetType="TabItem"&gt;
                &lt;Setter Property="Header" Value="{Binding Name}"/&gt;
                &lt;Setter Property="Content"&gt;
                   &lt;Setter.Value&gt;
                      &lt;u:VillageUserControl DataContext="{Binding}"/&gt;
                   &lt;/Setter.Value&gt;
                &lt;/Setter&gt;
            &lt;/Style&gt;
        &lt;/TabControl.ItemContainerStyle&gt;
  &lt;/TabControl&gt;
  </code></pre>
  
  <p>After I run it, it throws an exception: "Specified element is already the logical child of another element. Disconnect it first."</p>
  
  <p>Did I miss something? Please help me here...</p>