title: How can I define a ControlTemplate for my TabControl without breaking automated testing?
body: <p><strong>Summary</strong></p>
  
  <p>In my WPF application, I needed a TabControl with buttons on the left, so I defined a ControlTemplate with the layout I wanted and it worked fine.</p>
  
  <p>However, my tester's automated testing tool can't see any of the content of the tabs, including the currently selected tab.</p>
  
  <p><strong>Question: How can I keep my TabControl testable by the automated testing tools, while still defining the ControlTemplate?</strong></p>
  
  <hr>
  
  <p><strong>Details</strong></p>
  
  <p>I'm developing a WPF application using WPF 3.5<br>
  My tester is using an automated test tool called <a href="http://en.wikipedia.org/wiki/HP_QuickTest_Professional" rel="noreferrer">QTP</a><br>
  He says he can test anything you can see with <a href="http://msdn.microsoft.com/en-us/library/ms727247.aspx-" rel="noreferrer">UISpy.exe</a>  </p>
  
  <ul>
  <li>When I use a straight TabControl with no Template applied, UISpy can see the content of the currently selected tab.</li>
  <li>However, when I use a ContentTemplate to change the layout (code shown below), UISpy can still see the tab headers... but it cannot see the content.</li>
  </ul>
  
  <p>Sample WPF application (Xaml):</p>
  
  <pre><code>&lt;Window x:Class="TabControlTest.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="Tab Control Test"
          Height="300"
          Width="300"&gt;
      &lt;Window.Resources&gt;
          &lt;ControlTemplate x:Key="ButtonsOnLeftTabLayout"
                           TargetType="{x:Type TabControl}"&gt;
              &lt;DockPanel&gt;
                  &lt;StackPanel DockPanel.Dock="Left"
                              IsItemsHost="True" /&gt;
                  &lt;ContentPresenter Content="{TemplateBinding SelectedContent}" /&gt;
              &lt;/DockPanel&gt;
          &lt;/ControlTemplate&gt;
      &lt;/Window.Resources&gt;
      &lt;TabControl Template="{StaticResource ButtonsOnLeftTabLayout}"&gt;
          &lt;TabItem Header="Tab 1"&gt;
              &lt;StackPanel&gt;
                  &lt;Button HorizontalAlignment="Center"&gt;Button 1&lt;/Button&gt;
              &lt;/StackPanel&gt;
          &lt;/TabItem&gt;
          &lt;TabItem Header="Tab 2"&gt;
              &lt;StackPanel&gt;
                  &lt;Button HorizontalAlignment="Center"&gt;Button 2&lt;/Button&gt;
              &lt;/StackPanel&gt;
          &lt;/TabItem&gt;
      &lt;/TabControl&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>What my search has found so far:</p>
  
  <ul>
  <li>A bunch of stuff about having to write a custom TabControl with a custom AutomationPeer (e.g. MSFT answer to forum question <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2916a782-f466-425e-b03a-4fda8072e837" rel="noreferrer">UI Automation: accessing control in a ControlTemplate</a>, blog posting <a href="http://blogs.msdn.com/b/patrickdanino/archive/2009/11/11/custom-controls-and-ui-automation.aspx" rel="noreferrer">Custom Controls and UI Automation</a>). But my every instinct says that's crazy overkill, "there must be a simpler way!"</li>
  <li>Some suggestions about giving the ContentPresenter a Name, or x:Name, or AutomationProperties.AutomationId -- none of which have any effect</li>
  </ul>
  
  <p><em>(After search I finally found answer but it took longer than I thought it should have, and the AutomationPeer early findings were indeed incorrect, so I'm writing this as a SO question and self-answer, in case it helps anyone else in future)</em></p>