WPF TabControl: load all tabs when window loads

I have a WPF form with TabControl containing 2 TabItems. I have a check on the controls on one tab, which provides for the forced hang of the controls on another tab. However, when the user switches to the second tab, he does not display a red square around the checked control, because the contents of the tab are not displayed until the TabItem is viewed for the first time. Is there a way to load the contents of all tabs when the window loads?

Update:

I figured out a way to do this, but it feels very hacked. I added the following code to the MainWindow_OnLoaded event handler in my code:

for (var tabIndex = MainTabControl.Items.Count - 1; tabIndex >= 0; tabIndex--)
{
    MainTabControl.SelectedIndex = tabIndex;
    MainTabControl.UpdateLayout();
}

This code simply looks at all the TabControl tabs and sets them as the active tab and updates the layout. This forces all content to initialize. All this happens before the window appears, so the user does not see the change. I only have 2 tabs in my TabControl, but I could see that this is a little more inconvenient if there were more tabs.

+4
source share

All Articles