WPF TabControl: Load all tabs at window load

爱⌒轻易说出口 提交于 2019-12-10 13:35:56

问题


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.

来源:https://stackoverflow.com/questions/30337976/wpf-tabcontrol-load-all-tabs-at-window-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!