We do something similar, though we have the tab items already created (with no content) and show/hide as appropriate. When the tab item is selected, then we load the tab content.
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.OriginalSource != sender) return;
TabControl tabControl = (TabControl)sender;
TabItem tabItem = (TabItem)tabControl.SelectedItem;
if (!tabItem.HasContent)
AddTabContent(tabItem); // This will cause a refresh once the content is loaded.
else
Refresh(tabItem);
}
private void AddTabContent(TabItem tabItem)
{
IOptimusPage page = tabItem.Tag as IOptimusPage;
//This allows lazy loading of controls
if (page != null)
{
if (!tabItem.HasContent)
{
CustomerEngagementUserControl control = page.GetControl(DataContext as CustomerEngagementUIObject, Services);
tabItem.Content = control;
}
}
}
The tab item content is specified in the tab item tag, using pages which are responsible for creating the content.
<TabItem
Header="Personal Background"
Style="{StaticResource FirstBreadcrumbTabItem}"
x:Name="PersonalBackgroundTab">
<TabItem.Tag>
<Pages:FfnaPersonalBackgroundPage />
</TabItem.Tag>
</TabItem>
The page creates the control.
class FfnaPersonalBackgroundPage : IOptimusPage
{
#region IOptimusPage Members
public CustomerEngagementUserControl GetControl(CustomerEngagementUIObject dataContext, CustomerEngagementServices services)
{
CustomerEngagementUserControl control = new FfnaPersonalBackgroundControl();
control.DataContext = dataContext;
control.Services = services;
return control;
}
#endregion
}
You could use a similar technique to create your tab items on the fly.