I have a WPF form with a TabControl containing 2 TabItems. I have validation occuring on controls on one tab that enforce dependent validation of controls on the other tab. However, when the user switches to the second tab, it does not show any the red square around the validated control because the tab contents aren't rendered until that TabItem is viewed for the first time. Is there a way to load the contents of all tabs when the Window is loaded?
Update:
I figured out a way to do this, but it feels very hacky. I added the following code to the MainWindow_OnLoaded event handler in my code behind:
for (var tabIndex = MainTabControl.Items.Count - 1; tabIndex >= 0; tabIndex--)
{
MainTabControl.SelectedIndex = tabIndex;
MainTabControl.UpdateLayout();
}
This code simply loops through all tabs in the TabControl and sets them as the active tab and updates the layout. That forces all the contents to initialize. It all happens before the window appears so the user doesn't see the change. I only have 2 tabs in my TabControl, but I could see this being a bit more awkward if there were more tabs.
for
loop into a dispatcher call, e.g.,Dispatcher.BeginInvoke((Action)(() => { /* your update code */ }));
. – Byroad