Woodgnome
Woodgnome

Reputation: 2391

Why does my WPF TabControl automatically switch tab?

In a TabControl with 3 tabs:

  1. Tab1 contains a "Next" button.
  2. When "Next" is clicked TabControl is set to display Tab2 and a timer starts (2 seconds)
  3. After the timer ends TabControl is set to display Tab3.

If the window containing the TabControl does not have focus when #3 happens, it will switch back to display Tab2 when it receives focus. Why?

MainWindow 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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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)(() =>
            {
                Thread.Sleep(2000);
            }));

            MainTabControl.SelectedItem = Tab3;
        }
    }
}

WPF code:

<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">
    <TabControl Name="MainTabControl">
        <TabItem Name="Tab1">
            <TabItem.Header>
                <TextBlock Text="Tab 1" />
            </TabItem.Header>
            <StackPanel>
                <TextBlock Text="Tab 1 content" />
                <Button Name="NextBtn" Click="NextBtn_Click" Content="Next tab" Width="50" HorizontalAlignment="Right" />
            </StackPanel>
        </TabItem>

        <TabItem Name="Tab2">
            <TabItem.Header>
                <TextBlock Text="Tab 2" />
            </TabItem.Header>
            <StackPanel>
                <TextBlock Text="Tab 2 content" />
            </StackPanel>
        </TabItem>

        <TabItem Name="Tab3">
            <TabItem.Header>
                <TextBlock Text="Tab 3" />
            </TabItem.Header>
            <StackPanel>
                <TextBlock Text="Tab 3 content" />
            </StackPanel>
        </TabItem>
    </TabControl>
</Window>

Full project: https://ufile.io/165121

Upvotes: 3

Views: 1626

Answers (1)

ganchito55
ganchito55

Reputation: 3607

I debuged your program with Snoop and I saw that when you call to MainTabControl.SelectedItem = Tab3; and you had the focus in other window, the tab2 is focused so when you select the program's window, the tab2 is showed.

I fixed it adding the second line:

MainTabControl.SelectedItem = Tab3;
((TabItem) MainTabControl.SelectedItem).Focus();

I hope this can help you.

Upvotes: 4

Related Questions