This project is read-only.
1
Vote

No item selected after removal of currently selected TabItem in TabControl

description

Hello, in a Windows WPF application I used System.Windows.Controls.TabControl and ran into unexpected behavior. There might be a problem with TabControl, when currently selected TabItem is removed from Items collection no tab item is selected after that if the call to Items.Remove method came from another thread, example:
            TabControl tc = new TabControl();
            var tab1 = new TabItem() { Header = "tab1" };
            tc.Items.Add(tab1);
            var tab2 = new TabItem() { Header = "tab2" };
            tc.Items.Add(tab2);
            var tab3 = new TabItem() { Header = "tab3" };
            tc.Items.Add(tab3);

            tc.SelectedIndex = 1;

            grid.Children.Add(tc);

            // Diferent thread - no tab will be selected after removal of currently selected tab2
            Task t = new Task(
                () =>
                {
                    var Tab = tab2;
                    ((TabControl)Tab.Parent).Items.Remove(Tab);
                }
            );
            t.Start();

            //// Same thread - tab1 will be selected after removal of currently selected tab2
            //var Tab = tab2;
            //((TabControl)Tab.Parent).Items.Remove(Tab);
Attached is the screenshot of TabControl after removal of currently selected item from another thread.

file attachments

comments