Unless I’m mistaken (which I hope I am) there is a bug with the TabControl related to the visibility of TabItems.
Here’s XAML that reproduces the bug.
<UserControl x:Class="TabControl_bug.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:TabControl>
<sdk:TabItem Header="tabItem1" Visibility="Collapsed">
<TextBlock Text="TabItem1 which should not be visible" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</sdk:TabItem>
<sdk:TabItem Header="tabItem2">
<TextBlock Text="TabItem2 which should be visible" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</sdk:TabItem>
</sdk:TabControl>
</Grid>
</UserControl>
When you run this code, you’ll see that TabItem2 is not “selected” and so the content that is displayed is
TabItem 1 which should not be visible
Once you select the tab, then of course the TabItem2 content is displayed, and there’s no way of getting back to tabItem1, but the issue is in the inital display.
If I set the SelectedIndex property to 1 the correct content is displayed. Howerver I don’t know in the XAML which the of the Tabs should be selected first.
What workarounds are possible for this issue. Ideally the tabcontrol have preselected it’s first visable tabitem.
I’ve found next solution.For your sample in MainPage constructor:
Also you can do that on Loaded event.
Unfortunately, TabControl.SelectedContent property doesn’t have public setter so you can set SelectedContentProperty directly.
EDIT:
Behavior for this functionality:
Using sample: