title: Why does my WPF TabControl automatically switch tab?
body: <p>In a <code>TabControl</code> with 3 tabs:</p>
  
  <ol>
  <li><code>Tab1</code> contains a "Next" button.</li>
  <li>When "Next" is clicked <code>TabControl</code> is set to display <code>Tab2</code> and a timer starts (2 seconds)</li>
  <li>After the timer ends <code>TabControl</code> is set to display <code>Tab3</code>.</li>
  </ol>
  
  <p>If the window containing the <code>TabControl</code> does not have focus when #3 happens, it will switch back to display <code>Tab2</code> when it receives focus. Why?</p>
  
  <p><code>MainWindow</code> code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace WpfApplication1
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void NextBtn_Click(object sender, RoutedEventArgs e)
          {
              DoStuff();
          }
  
          private async void DoStuff()
          {
              MainTabControl.SelectedItem = Tab2;
  
              await Task.Factory.StartNew((Action)(() =&gt;
              {
                  Thread.Sleep(2000);
              }));
  
              MainTabControl.SelectedItem = Tab3;
          }
      }
  }
  </code></pre>
  
  <p>WPF code:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525" Topmost="True"&gt;
      &lt;TabControl Name="MainTabControl"&gt;
          &lt;TabItem Name="Tab1"&gt;
              &lt;TabItem.Header&gt;
                  &lt;TextBlock Text="Tab 1" /&gt;
              &lt;/TabItem.Header&gt;
              &lt;StackPanel&gt;
                  &lt;TextBlock Text="Tab 1 content" /&gt;
                  &lt;Button Name="NextBtn" Click="NextBtn_Click" Content="Next tab" Width="50" HorizontalAlignment="Right" /&gt;
              &lt;/StackPanel&gt;
          &lt;/TabItem&gt;
  
          &lt;TabItem Name="Tab2"&gt;
              &lt;TabItem.Header&gt;
                  &lt;TextBlock Text="Tab 2" /&gt;
              &lt;/TabItem.Header&gt;
              &lt;StackPanel&gt;
                  &lt;TextBlock Text="Tab 2 content" /&gt;
              &lt;/StackPanel&gt;
          &lt;/TabItem&gt;
  
          &lt;TabItem Name="Tab3"&gt;
              &lt;TabItem.Header&gt;
                  &lt;TextBlock Text="Tab 3" /&gt;
              &lt;/TabItem.Header&gt;
              &lt;StackPanel&gt;
                  &lt;TextBlock Text="Tab 3 content" /&gt;
              &lt;/StackPanel&gt;
          &lt;/TabItem&gt;
      &lt;/TabControl&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Full project: <a href="https://ufile.io/165121" rel="nofollow noreferrer">https://ufile.io/165121</a></p>