Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1042749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:33:31+00:00 2026-05-16T15:33:31+00:00

I have a problem with the selected item in WPF-TabControl that could be reproduced

  • 0

I have a problem with the selected item in WPF-TabControl that could be reproduced with the following simplified code:

If a new TabItem is created and selected through the Click-event of the button in the second window, the tab is created, added and selected. When the second window will be closed, the selected item of the tab-control is reset to the last selected item. The problem occurs in my MVVM-app and it is independent of the Items-collection. I can use the ItemsSource or the Items-Collection, it happens always.

Has someone an idea what happens here or has a nice workaround?

TabControl tabControl = new TabControl() ;
tabControl.Items.Add(new TabItem { Header="InitialTabItem"});
((TabItem)tabControl.Items[0]).Focus();
Window mainWindow = new Window() { Content=tabControl,Title="TabControl-Window"};
mainWindow.Show();            
Button addButton = new Button() { Content="AddTabItem"};
addButton.Click += (o, e) => {
        TabItem newTabItem=new TabItem(){Header=DateTime.Now.ToLongTimeString()};
        tabControl.Items.Add(newTabItem);                
        tabControl.SelectedItem = newTabItem;                
};
Window directorWindow = new Window() { Owner = mainWindow ,Content=addButton,Height=80,Width=200,Title="DirectorWindow"};
directorWindow.Show();

Update

It seems to be as always when I have bigger problems with WPF, to be an issue with focus-management. If I change the creation-code as follows, it works:

TabItem newTabItem=new TabItem(){Header=DateTime.Now.ToLongTimeString()};
tabControl.Items.Add(newTabItem);                    
Dispatcher.BeginInvoke(new Action(delegate{
    newTabItem.Focus();
    tabControl.SelectedItem = newTabItem;
}), System.Windows.Threading.DispatcherPriority.Input, null);

However it looks not very confidently to me. Some ideas?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:33:31+00:00Added an answer on May 16, 2026 at 3:33 pm

    Yeah, TabControl behaves strange sometimes. In our project we had to create a subclass and override some methods to workaround another bug in it.

    In your case, everything seems to work if you focus the TabControl itself before focusing the TabItem:

            var tabControl = new TabControl();
            var tabItem = new TabItem { Header = "InitialTabItem" };
            tabControl.Items.Add(tabItem);
            tabControl.Focus();
            tabItem.Focus();
            Window mainWindow = new Window() { Content = tabControl, Title = "TabControl-Window" };
            mainWindow.Show();
            Button addButton = new Button() { Content = "AddTabItem" };
            addButton.Click += (o, args) =>
            {
                TabItem newTabItem = new TabItem() { Header = DateTime.Now.ToLongTimeString() };
                tabControl.Items.Add(newTabItem);
                tabControl.SelectedItem = newTabItem;
            };
            Window directorWindow = new Window() { Owner = mainWindow, Content = addButton, Height = 80, Width = 200, Title = "DirectorWindow" };
            directorWindow.Show();
    

    Update 1. See comments — the original code causes bad side effects.

            var tabControl = new TabControl();
            var tabItem = new TabItem { Header = "InitialTabItem" };
            tabControl.Items.Add(tabItem);
            Window mainWindow = new Window() { Content = tabControl, Title = "TabControl-Window" };
            mainWindow.Show();
            tabControl.Focus();
            tabItem.Focus();
            Button addButton = new Button() { Content = "AddTabItem" };
            addButton.Click += (o, args) =>
            {
                TabItem newTabItem = new TabItem() { Header = DateTime.Now.ToLongTimeString() };
                tabControl.Items.Add(newTabItem);
                tabControl.SelectedItem = newTabItem;
            };
            Window directorWindow = new Window() { Owner = mainWindow, Content = addButton, Height = 80, Width = 200, Title = "DirectorWindow" };
            directorWindow.Show();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to WPF. I have this code <Window x:Class=ElementBinding.MultipleBindings xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MultipleBindings
I have a problem, I need to get the selected Item from a ContextMenu
Hi all i am new to WPF and i have a small problem ,i
This may sound trivial but i have a problem with setting selected item in
I have a problem with my NSTextView. My textView is selected and editable when
I have quastion about DataGrid in WPF .NET 4. Here is XAML code with
I am new to WPF, even though I have tried to read as much
I have encountered a major problem for myself in the learning process of WPF
I have this problem. I use caliburn micro in WPF. In view I have
I have a following problem. I have some items in my treeview which are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.