title: WPF TabControl: Children unloaded when other tab is selected
body: <p>Is it possible to prevent WPF TabControl from unloading the children of a TabItem when selecting an other tab?</p>
  
  <p>The problem I'm facing is similar to the one described here:
  <a href="https://stackoverflow.com/questions/3601125/wpf-tabcontrol-preventing-unload-on-tab-change">WPF TabControl - Preventing Unload on Tab Change?</a></p>
  
  <p>The solution provided there seems to work only if the TabControl is data bound.
  It doesn't work if you add TabItems:</p>
  
  <pre><code>&lt;local:TabControlEx&gt;
  
      &lt;TabItem Header="First Tab"&gt;
          &lt;TreeView ItemsSource="{Binding TreeNodes}" Unloaded="treeView_Unloaded"&gt;
              &lt;TreeView.Resources&gt;
                  &lt;DataTemplate DataType="{x:Type local:NodeViewModel}"&gt;
                      &lt;TextBlock Text="{Binding NodeName}" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/TreeView.Resources&gt;
  
              &lt;TreeView.ItemContainerStyle&gt;
                  &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
                      &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /&gt;
                  &lt;/Style&gt;
              &lt;/TreeView.ItemContainerStyle&gt;
          &lt;/TreeView&gt; 
      &lt;/TabItem&gt;
  
      &lt;TabItem Header="Second Tab"&gt;
          &lt;TextBlock Name="txText2"&gt;Second Text 2&lt;/TextBlock&gt;
      &lt;/TabItem&gt;
  
  &lt;/local:TabControlEx&gt;
  </code></pre>
  
  <p>When you select "Second Tab", treeView_Unloaded is triggered. </p>
  
  <p>Background:
  In my real application, one of the TabItems contains a UserControl containing a data bound TreeView: TreeViewItem.IsSelected is bound to a property. Setting the Property bound to IsSelected selects the corresponding TreeViewItem. If the user switches to another tab, the TreeView is unloaded (removed from VisualTree). In that situation, setting IsSelected to true on any node not having a corresponding TreeViewItem causes the TreeView to misbehave - probably because the TreeView does not create a TreeViewItem for the node which should be selected, because the TreeView is not currently part of the visual tree. So what I want to achieve is that I can select any other node by setting IsSelected to true - even if the TreeView is currently on a non-visible tab. Moving the TreeView to some place outside the TabControl seems to resolve the problem - even if it is inside a panel which has visibility collapsed. So, visibility doesn't seem to be the problem, but the fact that the TreeView is not currently part of the visual tree.</p>