title: WPF TabControl: Load all tabs at window load
body: <p>I have a WPF form with a TabControl containing 2 TabItems.  I have validation occuring on controls on one tab that enforce dependent validation of controls on the other tab.  However, when the user switches to the second tab, it does not show any the red square around the validated control because the tab contents aren't rendered until that TabItem is viewed for the first time.  Is there a way to load the contents of all tabs when the Window is loaded?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I figured out a way to do this, but it feels very hacky.  I added the following code to the MainWindow_OnLoaded event handler in my code behind:</p>
  
  <pre><code>for (var tabIndex = MainTabControl.Items.Count - 1; tabIndex &gt;= 0; tabIndex--)
  {
      MainTabControl.SelectedIndex = tabIndex;
      MainTabControl.UpdateLayout();
  }
  </code></pre>
  
  <p>This code simply loops through all tabs in the TabControl and sets them as the active tab and updates the layout.  That forces all the contents to initialize.  It all happens before the window appears so the user doesn't see the change.  I only have 2 tabs in my TabControl, but I could see this being a bit more awkward if there were more tabs.</p>