Tab Control In WPF

In this article I am going to explain how to implement tabcontrol in WPF.

The Tab control is a common UI element that has been around for some time. It makes a convenient way to organize your window when there is more than could realistically fit and still be comprehensible. Tab Control is easier in Windows Presentation Foundation. Thanks to XAML, you can build a tab control from scratch with markup codes.

Two elements play main roles in building a tab control,

  • TabControl and
  • TabItem

TabControl is the container of one or more TabItem elements as follows.

  1. <TabControl>  
  2.     <TabItem Header="Tab 1">xyz</TabItem>  
  3.     <TabItem Header="Tab 2">abc</TabItem>  
  4. </TabControl>  
Each TabControl can contain a collection of TabItem elements. TabItem has two specific attributes. Header is the string value that you see on top of each tab and IsSelected is a Boolean value that specifies if a tab is selected. Apparently only one tab can be selected at a time otherwise the first tab in the list will be selected.

In WPF, Tabs are very easy to implement. Create a new WPF Window, remove the default Grid tags, and add the following XAML: 
  1. <TabControl>  
  2.     <TabItem Header="Tab 1">xyz</TabItem>  
  3.     <TabItem Header="Tab 2">abc</TabItem>  
  4. </TabControl>  
For example,

Window1.xaml
  1. <Window x:Class="TabControl.Window1" xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title="Window1" Height="300" Width="500">  
  2.     <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">  
  3.         <StackPanel>  
  4.             <Border Background="#99FFFFFF" BorderBrush="#CCCCFF" BorderThickness="2" HorizontalAlignment="Left" Padding="20" Margin="20,10,0,0" Width="400">  
  5.                 <TabControl Name="Tabs1" TabStripPlacement="Top" Background="LightGray" BorderBrush="Blue" Margin="10,10,0,5">  
  6.                     <TabItem Header="Name" IsSelected="True" Background="Gray">  
  7.                         <TextBlock Height="150" Width="200" Margin="0,10,120,0">  
  8.                             <Bold>Purushottam Rathore</Bold>  
  9.                         </TextBlock>  
  10.                     </TabItem>  
  11.                     <TabItem Header="Image" Background="Gray">  
  12.                         <Image Source="ballon.gif" Height="150" Width="200" />  
  13.                     </TabItem>  
  14.                     <TabItem Header="Hotels" Background="Gray">  
  15.                         <Image Source="r.jpg" Height="150" Width="200" />  
  16.                     </TabItem>  
  17.                     <TabItem Header="Contact Us" Background="Gray">  
  18.                         <Grid Height="150" Width="300">  
  19.                             <Grid.RowDefinitions>  
  20.                                 <RowDefinition></RowDefinition>  
  21.                                 <RowDefinition></RowDefinition>  
  22.                                 <RowDefinition></RowDefinition>  
  23.                             </Grid.RowDefinitions>  
  24.                             <Grid.ColumnDefinitions>  
  25.                                 <ColumnDefinition Width="*"></ColumnDefinition>  
  26.                                 <ColumnDefinition Width="2*"></ColumnDefinition>  
  27.                             </Grid.ColumnDefinitions>  
  28.                             <Label HorizontalAlignment="Right" Margin="0,12,0,0" Width="42.033"> Name:</Label>  
  29.                             <Label Grid.Row="1" HorizontalAlignment="Right" Margin="0,14,0,0" Width="40.097"> Email:</Label>  
  30.                             <TextBox Name="Name" Grid.Column="1" Margin="0,0,0,13" />  
  31.                             <TextBox Name="Email" Grid.Column="1" Grid.Row="1" Margin="0,14,0,0" />  
  32.                             <ButtonGrid.Column="1" Grid.Row="2" Width="75" HorizontalAlignment="Left" Name="CoolTabButton" Click="CoolTabButton_Click" Margin="0,15,0,12">OK</Button>  
  33.                         </Grid>  
  34.                     </TabItem>  
  35.                 </TabControl>  
  36.             </Border>  
  37.         </StackPanel>  
  38.     </ScrollViewer>  
  39. </Window> 
Selected property
Figure 1

TabItem contain the IsSelected property. if you write IsSelected="True" then that tab will select by default. In this example I have written IsSelected="True" in first tab so the first tab named "Name" is selected by default (in the above figure).

Window1.xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14. namespace TabControl {  
  15.     publicpartialclassWindow1: Window {  
  16.         public Window1() {  
  17.             InitializeComponent();  
  18.         }  
  19.         privatevoid CoolTabButton_Click(object sender, RoutedEventArgs e) {  
  20.             TabItem ti = Tabs1.SelectedItem asTabItem;  
  21.             MessageBox.Show("This is " + ti.Header + " tab");  
  22.         }  
  23.     }  
  24. }  
Now debug the application, you will get the following output.

WPF Output 
Figure 2

WPF Output 
Figure 3

WPF Output 
Figure 4

WPF Output 
Figure 5

TabStripPlacement is an attribute that lets you  change the position of tab strip (default is Top). There are four attributes of TabStripPlacement.
  • Top
  • Left
  • Right
  • Bottom

If you write TabStripPlacement="Bottom" then you will get the following output:

Tabs are at bottom position 
Figure 6: Tabs are at bottom position

How to add a Context Menu to a Tab Control

TabItem.ContextMenu is used to add a ContextMenu to a TabItem. The following code snippet creates a TabControl where the first TabItem has a ContextMenu with three Menu items.

  1. <TabControl>  
  2.     <TabItem Name="ColorTabItem" Header="Color Tab">  
  3.         <TabItem.ContextMenu>  
  4.             <ContextMenu MenuItem.Click="ContextMenuClickEventHandler">  
  5.                 <MenuItem Header="Red" Name="RedMenuItem" />  
  6.                 <MenuItem Header="Blue" Name="BlueMenuItem" />  
  7.                 <MenuItem Header="Orange" Name="OrangeMenuItem" />  
  8.             </ContextMenu>  
  9.         </TabItem.ContextMenu>  
  10.         <TabItem.Content> Tab Item data here </TabItem.Content>  
  11.     </TabItem>  
  12.     <TabItem Name="ShapeTabItem" Header="Shape Tab"></TabItem>  
  13. </TabControl>  

Write this click event handler to your code behind.

  1. void ContextMenuClickEventHandler(object sender, RoutedEventArgs e) {  
  2.     if (e.Source == RedMenuItem) {  
  3.         ColorTabItem.Header = "Red Item";  
  4.         ColorTabItem.Foreground = Brushes.Red;  
  5.     } else if (e.Source == BlueMenuItem) {  
  6.         ColorTabItem.Header = "Blue Item";  
  7.         ColorTabItem.Foreground = Brushes.Blue;  
  8.     } else if (e.Source == OrangeMenuItem) {  
  9.         ColorTabItem.Header = "Orange Item";  
  10.         ColorTabItem.Foreground = Brushes.Orange;  
  11.     }  
  12. }  

The output looks like this. Download the attached project for more details.

TabContext Menu output