Using XAML TabControl in WPF

The XAML TablControl element represents a tab control. In this article, we will see how to use a TabControl in WPF.

A Tab Control has tab items and each tab item represents a container that is used to host other controls. A typical example of a Tab control is the Visual Studio designer as shown in Figure 1. If you click on the Window1.xaml tab item, you will see XAML code but if you click on Window1.xaml.cs, you will see C# code behind.


Figure 1

Create a Tab Control

The TabControl element in XAML represents a Tab Control.

  1. <TabControl />  
The code snippet in Listing 1 creates a Tab Control and sets it height, width, margin and alignment. The code also adds a TabItem.
  1. <TabControl Height="238" HorizontalAlignment="Left" Margin="12,12,0,0"   
  2.             Name="tabControl1" VerticalAlignment="Top" Width="445">  
  3.     <TabItem Header="tabItem1" Name="tabItem1">  
  4.     </TabItem>  
  5. </TabControl>  
Listing 1

The output of Listing 1 looks as in Figure 2.


Figure 2

Adding Tab Items

A Tab Control is nothing without a Tab Item. The <TabItem \> element in XAML represents a Tab Item. The Header property of a TabItem represents the header text of the header. The following code creates a TabItem with the header text “Circle”.
  1. <TabItem Header="Circle">                 
  2. </TabItem>  
A tab item can host other XAML controls similar to a panel or grid control. For example, the following code adds a StackPanel to the tab item and on that StackPanel, it creates a circle with height and width 100.
  1. <TabItem  Header="Circle">  
  2.    <StackPanel>  
  3. <Ellipse Height="100" Width="100" StrokeThickness="5" Stroke="black"  
  4.       Fill="gold"/>  
  5.    </StackPanel>  
  6. </TabItem >  
Using the same preceding approach, I add three tab items to the tab control (Listing 2) called Circle, Rectangle and Polygon.
  1. <TabControl Height="150" FontFamily="Verdana" FontSize="12" >  
  2.             <TabControl.Background>  
  3.                 <SolidColorBrush Color="Green" Opacity="0.30"/>  
  4.             </TabControl.Background>  
  5.             <TabItem  Header="Circle">  
  6.                 <StackPanel>  
  7.                     <Ellipse Height="100" Width="100" StrokeThickness="5" Stroke="black"  
  8.       Fill="gold"/>  
  9.                 </StackPanel>  
  10.             </TabItem >  
  11.             <TabItem  Header="Rectangle">  
  12.                 <StackPanel>  
  13.                     <Rectangle Fill="Yellow" Width="100" Height="100" Stroke="Blue" StrokeThickness="5">  
  14.                     </Rectangle>  
  15.                 </StackPanel>  
  16.             </TabItem >  
  17.             <TabItem  Header="Polygon">  
  18.                 <StackPanel>  
  19.                     <Polygon Points="100,50 50,100 150,100 100,50 100,30"  
  20.        Stroke="green" StrokeThickness="3" Fill="Yellow"/>  
  21.                 </StackPanel>  
  22.             </TabItem >  
  23. </TabControl>  
Listing 2

If I run the application, the Circle tab shows me a circle that looks as in Figure 3.


Figure 3

If I click on Rectangle and Polygon, the output looks as in Figure 4 and Figure 5 respectively.


Figure 4


Figure 5

Formatting Tab Items


Let's get a little creative. How about making our tab headers and tab control areas look nicer.

In a Tab Control, both a Tab Header and Tab Item are loosely coupled and we can treat them as any other WPF control. That said, we can place any children controls on them, format them and do whatever we want. So if we want to place a DataGrid control in a TabItem header, we can do that.

Now let's format our Tab Item Header and Tab Item container area. We will format our Tab Item that looks as in Figure 6, where we will use multiple colors for the background and change the text of the Tab Item Header and we will also change the background of the Tab Item container area to a gradient background.


Figure 6

To do this, I placed a StackPanel on TabItem.Header and set its background to a LinearGradientBrush. Then, I placed an Ellipse and a TextBlock on the TabItem Header.

Then as the content of the TabItem, I placed a StackPanel and changed its background to a LinearGradientBrush. See Listing 3.
  1. <TabItem >  
  2.         <!-- Create Tab Header -->  
  3.         <TabItem.Header>  
  4.             <!-- Place a StackPanel on TabHeader so we can place multiple controls on it -->  
  5.             <StackPanel Orientation="Horizontal">  
  6.                 <StackPanel.Background>  
  7.                     <LinearGradientBrush StartPoint="0,0" EndPoint="1,0" >  
  8.                         <LinearGradientBrush.GradientStops>  
  9.                             <GradientStop Offset="0" Color="Orange"/>  
  10.                             <GradientStop Offset="0.25" Color="Purple"/>  
  11.                             <GradientStop Offset="0.5" Color="Violet"/>  
  12.                             <GradientStop Offset="0.75" Color="Green"/>  
  13.                         </LinearGradientBrush.GradientStops>  
  14.                     </LinearGradientBrush>  
  15.                 </StackPanel.Background>                                  
  16.                 <Ellipse Height="20" Width="20" Stroke="Black" StrokeThickness="2" Fill="Yellow"/>  
  17.                 <TextBlock Text="Circle" Margin="1,1,1,1" VerticalAlignment="Center"  
  18.                            FontFamily="Georgia" FontSize="18" FontWeight="Bold" />  
  19.             </StackPanel>  
  20.         </TabItem.Header>  
  21.       
  22.         <!-- Place a StackPanel on TabItem to place children controls -->  
  23.         <StackPanel>  
  24.             <StackPanel.Background>  
  25.                 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >  
  26.                     <LinearGradientBrush.GradientStops>  
  27.                         <GradientStop Offset="0" Color="Orange"/>  
  28.                         <GradientStop Offset="0.25" Color="Purple"/>  
  29.                         <GradientStop Offset="0.5" Color="Violet"/>  
  30.                         <GradientStop Offset="0.75" Color="Green"/>  
  31.                     </LinearGradientBrush.GradientStops>  
  32.                 </LinearGradientBrush>  
  33.             </StackPanel.Background>  
  34.             <Ellipse Height="100" Width="100" StrokeThickness="5" Stroke="black" Fill="gold"/>  
  35.         </StackPanel>  
  36.     </TabItem >   
Listing 3

Note: If you would like to load images at the top of text of the header, simply remove Orientation="Horizontal" from the stack panel in the preceding code. The default orientation is vertical.

Display Image as Tab Item Header


You may display an image in a Tab Item Header by placing an Image control as TabItem.Header as in the following.
  1. <TabItem.Header>  
  2.     <Image Source="Flower.jpg" Width="50" />  
  3. </TabItem.Header>    
The Tab Item Header with image looks as in Figure 7.


Figure 7

Position Tab Items

The TabStripPlacement property of a TabControl is used to position Tab Items. The value of TabStripPlacement can be Left, Top, Bottom and Right. The following code snippet sets the Tab Items position to bottom.
  1. <TabControl Height="238" HorizontalAlignment="Left" Margin="12,12,0,0"   
  2.     Name="tabControl1" VerticalAlignment="Top" Width="445"  
  3.            SelectionChanged="tabControl1_SelectionChanged"  
  4.            TabStripPlacement="Bottom">  
The TabControl with bottom positioned Tab Items looks as in Figure 8.


Figure 8

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>  
  11.             Tab Item data here  
  12.         </TabItem.Content>  
  13.     </TabItem>  
  14.     <TabItem Name="ShapeTabItem" Header="Shape Tab"></TabItem>  
  15. </TabControl>  
Write this click event handler in your code behind.
  1. void ContextMenuClickEventHandler(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (e.Source == RedMenuItem )  
  4.     {                  
  5.         ColorTabItem.Header = "Red Item";  
  6.         ColorTabItem.Foreground = Brushes.Red;  
  7.     }  
  8.     else if (e.Source == BlueMenuItem )  
  9.     {  
  10.         ColorTabItem.Header = "Blue Item";  
  11.         ColorTabItem.Foreground = Brushes.Blue;  
  12.     }  
  13.     else if (e.Source == OrangeMenuItem )  
  14.     {  
  15.         ColorTabItem.Header = "Orange Item";  
  16.         ColorTabItem.Foreground = Brushes.Orange;  
  17.     }  
  18. }  
The output looks like this. Download the attached project for more details.



Summary

In this article, we saw how to create and use a TabControl in a WPF application. We also saw how to make a tab item header and its contents more interactive by simply adding a few lines of code to our XAML file.

 


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.