title: WTF WPF TabControl?
body: <p>I believe this to be a bug in WPF (v4.0 if it matters), but it is late, and maybe I'm missing something.</p>
  
  <p>I am binding to a fake example for illustrative purposes:</p>
  
  <pre><code>    &lt;x:Array x:Key="SampleItems" Type="sys:String"&gt;
          &lt;sys:String&gt;Foo&lt;/sys:String&gt;
          &lt;sys:String&gt;Bar&lt;/sys:String&gt;
          &lt;sys:String&gt;Baz&lt;/sys:String&gt;
      &lt;/x:Array&gt;
  </code></pre>
  
  <p>This works and displays three tabs with the same header and content:</p>
  
  <pre><code>&lt;TabControl ItemsSource="{StaticResource SampleItems}"&gt;
              &lt;TabControl.ItemContainerStyle&gt;
                  &lt;Style TargetType="TabItem"&gt;
                      &lt;Setter Property="Header" Value="{Binding}" /&gt;
                      &lt;Setter Property="Content" Value="{Binding}" /&gt;
                  &lt;/Style&gt;
              &lt;/TabControl.ItemContainerStyle&gt;
          &lt;/TabControl&gt;
  </code></pre>
  
  <p>However, this throws an exception with the message "Error 10 Specified element is already the logical child of another element. Disconnect it first.":</p>
  
  <pre><code>&lt;TabControl ItemsSource="{StaticResource SampleItems}"&gt;
      &lt;TabControl.ItemContainerStyle&gt;
          &lt;Style TargetType="TabItem"&gt;
              &lt;Setter Property="Header"&gt;
                  &lt;Setter.Value&gt;
                      &lt;!-- Anything here causes this problem. --&gt;
                      &lt;TextBlock Text="{Binding}"/&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
              &lt;Setter Property="Content" Value="{Binding}" /&gt;
          &lt;/Style&gt;
      &lt;/TabControl.ItemContainerStyle&gt;
  &lt;/TabControl&gt;
  </code></pre>
  
  <p>It is important to note that this is reproducible with any text in either TextBlock.  In fact, I can replace the header TextBlock with any XAML and get this message.  I'm at a loss to explain this.  Any ideas, or is this just a bug?</p>
  
  <p>The problem appears in the VS designer, but here is a portion of the relevant stack trace at runtime as well:</p>
  
  <pre><code>   at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
     at System.Windows.FrameworkElement.AddLogicalChild(Object child)
     at System.Windows.Controls.HeaderedContentControl.OnHeaderChanged(Object oldHeader, Object newHeader)
     at System.Windows.Controls.HeaderedContentControl.OnHeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
     at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
     at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
     at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
     at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry&amp; newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
     at System.Windows.StyleHelper.ApplyStyleOrTemplateValue(FrameworkObject fo, DependencyProperty dp)
     at System.Windows.StyleHelper.InvalidateContainerDependents(DependencyObject container, FrugalStructList`1&amp; exclusionContainerDependents, FrugalStructList`1&amp; oldContainerDependents, FrugalStructList`1&amp; newContainerDependents)
     at System.Windows.StyleHelper.DoStyleInvalidations(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle)
     at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style&amp; styleCache)
     at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
     at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
     at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
     at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
     at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry&amp; newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
     at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
     at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
     at System.Windows.Controls.ItemsControl.ApplyItemContainerStyle(DependencyObject container, Object item)
     at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
     at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
     at System.Windows.Controls.Panel.GenerateChildren()
  </code></pre>