TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

WPF TabControl Example: TabItem

This WPF article uses the TabControl. TabItems have Grid controls. It detects tab switches with SelectionChanged.

TabControl. Tabs are a user interface metaphor.

They separate content. In WPF, we use the TabControl to create a tabbed user interface. In each tab, we add sub-controls to a Grid. And each tab has a Header.

Based on:

.NET 4.5

Example. To begin, please drag a TabControl to your WPF window. By default, two TabItems are present. Change their "Header" properties to be something more descriptive. You can add a third TabItem by right-clicking and selecting "Add TabItem."

Next, we add some sub-controls to the TabItems. Try adding your favorite control. Here I add TextBlocks, and modify the Content in each one to be unique. You can add multiple controls to the Grid.

TextBlock

Note: By default, the Grid control within a TabControl has a Background property set. This accounts for the gray color.

Example markup: XAML

<Window x:Class="WpfApplication25.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="MainWindow" Height="350" Width="525">
    <Grid>
	<TabControl HorizontalAlignment="Left"
		    Height="299"
		    Margin="10,10,0,0"
		    VerticalAlignment="Top"
		    Width="497"
		    SelectionChanged="TabControl_SelectionChanged">
	    <TabItem Header="Cat">
		<Grid Background="#FFE5E5E5">
		    <TextBlock HorizontalAlignment="Left"
			       Margin="10"
			       TextWrapping="Wrap"
			       Text="Take pictures of me and put them on the Internet. Meow."
			       VerticalAlignment="Top"
			       Width="471"/>
		</Grid>
	    </TabItem>
	    <TabItem Header="Mouse">
		<Grid Background="#FFE5E5E5">
		    <TextBlock HorizontalAlignment="Left"
			       Margin="10"
			       TextWrapping="Wrap"
			       Text="I want some cheese."
			       VerticalAlignment="Top"
			       Width="471"/>
		</Grid>
	    </TabItem>
	</TabControl>
    </Grid>
</Window>

Example program: C#

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication25
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
	public MainWindow()
	{
	    InitializeComponent();
	}

	private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
	{
	    // ... Get TabControl reference.
	    var item = sender as TabControl;
	    // ... Set Title to selected tab header.
	    var selected = item.SelectedItem as TabItem;
	    this.Title = selected.Header.ToString();
	}
    }
}

SelectionChanged. In the example, we also use the SelectionChanged event handler. Add the SelectionChanged attribute and press tab—Visual Studio inserts the C# code. In TabControl_SelectionChanged, we get the TabControl reference.

And: We cast the SelectedItem to the TabItem type. Finally we set the Title of the Window to the Header of the TabItem.

Summary. Tabs are an intuitive user interface element. They are a real-world metaphor—this aids program usability. With nested Grid elements, we can add sub-controls to the TabControl. And we detect tab switching with SelectionChanged.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf