TabControl
The TabControl allows us to switch between different pages by means of tabs like the tabs in web navigators or the ribbon menu (which uses tabs) in Word Office for instance.
Here is an animation of what you can achieve :
To create this, we'll describe the entire control (TabControl) and each individual tab+page (TabItem). Here is an example :
1
<TabControl>
2
<TabItem Header="Circle" VerticalContentAlignment="Center">
3
<TextBlock Text="I am in the circle page !" HorizontalAlignment="Left" VerticalAlignment="Center"/>
4
</TabItem>
5
<TabItem>
6
<TabItem.Header>
7
<TextBlock VerticalAlignment="Center">Triangle</TextBlock>
8
</TabItem.Header>
9
<StackPanel>
10
<TextBlock Text="I am in the triangle page ! I'll put a button to show you that each page contains what you want." HorizontalAlignment="Left" VerticalAlignment="Center"/>
11
<Button>A button in the triangle page !</Button>
12
</StackPanel>
13
</TabItem>
14
<TabItem>
15
<TabItem.Header>
16
<TextBlock VerticalAlignment="Center">Square</TextBlock>
17
</TabItem.Header>
18
<StackPanel Orientation="Horizontal">
19
<TextBlock Text="Square : " HorizontalAlignment="Left" VerticalAlignment="Center"/>
20
<Rectangle Fill="Blue" Width="63" Height="41"/>
21
</StackPanel>
22
</TabItem>
23
</TabControl>
Copied!

If you want to customize the look&feel of the TabControl

Let's have a look at a customized TabControl :
The grey part is the TabItem... Yes, the TabItem includes the tab AND the page associated to the tab. The tab is called the headerof the TabItem. Moreover, given the way TabControl has been implemented, tabs are in a WrapPanel. Thus, if you want to color in blue (like this is done above) the empty bar of the tabbed bar, you must change the background color of the WrapPanel of the TabControl. Here is the code used to obtain the result above (Note the workaround used to color some tabs : this is due to the way the control is implemented. It might change in the future.)
1
<Window.Styles>
2
​
3
<Style Selector="TabControl">
4
<Setter Property="Background" Value="#F0F0F0"/>
5
<Setter Property="Height" Value="120"/>
6
</Style>
7
<Style Selector="TabControl WrapPanel">
8
<Setter Property="Background" Value="#2B579A"/>
9
</Style>
10
​
11
<Style Selector="TabItem">
12
<Setter Property="FontSize" Value="12"/>
13
<Setter Property="Height" Value="34"/>
14
<Setter Property="VerticalAlignment" Value="Center"/>
15
<Setter Property="Background" Value="#2B579A"/>
16
<Setter Property="Foreground" Value="#F0F0F0"/>
17
<Setter Property="Margin" Value="0 0 0 0"/>
18
<Setter Property="Padding" Value="10 0"/>
19
</Style>
20
<Style Selector="TabItem:pointerover /template/ ContentPresenter#PART_ContentPresenter">
21
<Setter Property="Background" Value="#124078"/>
22
</Style>
23
​
24
<Style Selector="TabItem:focus">
25
<Setter Property="Foreground" Value="#2B579A"/>
26
<Setter Property="Margin" Value="0 0 0 0"/>
27
<Setter Property="Padding" Value="10 0"/>
28
</Style>
29
<Style Selector="TabItem:focus /template/ ContentPresenter#PART_ContentPresenter">
30
<Setter Property="Background" Value="#f0f0f0"/>
31
</Style>
32
​
33
<Style Selector="TabItem:selected">
34
<Setter Property="Foreground" Value="#2B579A"/>
35
<Setter Property="Margin" Value="0 0 0 0"/>
36
<Setter Property="Padding" Value="10 0"/>
37
</Style>
38
<Style Selector="TabItem:selected /template/ ContentPresenter#PART_ContentPresenter">
39
<Setter Property="Background" Value="#f0f0f0"/>
40
</Style>
41
​
42
</Window.Styles>
Copied!

Binding support

Dynamically generated TabItems through binding are also supported. In this case bind Items property. The TabStrip header content is defined by ItemTemplate property, while TabItem's content is defined by ContentTemplate property.
The following example uses a TabItemModel array to represent two tabs. Let's first create the model.
1
public class TabItemModel
2
{
3
public string Header { get; }
4
public string Content { get; }
5
public TabItemModel(string header, string content)
6
{
7
Header = header;
8
Content = content;
9
}
10
}
Copied!
Create an array of two TabItemModel instances and bind it to the DataContext.
1
DataContext = new TabItemModel[] {
2
new TabItemModel("One", "Some content on first tab"),
3
new TabItemModel("Two", "Some content on second tab"),
4
};
Copied!
Finally create a TabControl and bind its Items property to the DataContext.
1
<TabControl Items="{Binding}">
2
<TabControl.ItemTemplate>
3
<DataTemplate>
4
<TextBlock Text="{Binding Header}" />
5
</DataTemplate>
6
</TabControl.ItemTemplate>
7
<TabControl.ContentTemplate>
8
<DataTemplate>
9
<DockPanel LastChildFill="True">
10
<TextBlock Text="This is content of selected tab" DockPanel.Dock="Top" FontWeight="Bold" />
11
<TextBlock Text="{Binding Content}" />
12
</DockPanel>
13
</DataTemplate>
14
</TabControl.ContentTemplate>
15
</TabControl>
Copied!

Reference

​TabControl​

Source code

​TabControl.cs​
Last modified 3mo ago