Example of Customized TabItem of TabControl in WPF

December 22, 2015 0 Comments

Customized Tab Item Example:
Tab Item is the item control of Tab control. In this article, I will show you, how to add images, TextBlock and any other control inside TabControl. Also I will show you how to move Tab Control's tabs using buttons like Pre, Next, Get Item of Selected tab. When we press Previous button then next tab move to previous button. Similarly for all buttons. 

<Window x:Class="WpfApplication9.tabcon"
        Title="tabcon" Height="300" Width="300">
            <TabItem Header="First Item">
                <Label Content="Items in the first tab"/>
            <TabItem Header="Second Item"/>
            <TabItem Header="Third Item"/>
            <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom">
                <Button Name="prebutton" Click="prebutton_Click" Height="36" Width="54">Pre</Button>
                <Button Name="Nextbutton" Click="Nextbutton_Click"  Height="36" Width="54">Next</Button>
                <Button Name="selectbutton" Click="selectbutton_Click"  Height="36" Width="54">select</Button>
            <TabControl Name="sample">
                <TabItem Header="First Item">
                    <Label Content="Content in First tab"/>
                        <TextBlock Text="Second Tab" Foreground="Red"/>
                <TabItem Header="Third Tab"/>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication9
    /// <summary>
    /// Interaction logic for tabcon.xaml
    /// </summary>
    public partial class tabcon : Window
        public tabcon()

        private void prebutton_Click(object sender, RoutedEventArgs e)
            int nindex = sample.SelectedIndex - 1;
            if (nindex < 0)
                nindex = sample.Items.Count - 1;
            sample.SelectedIndex = nindex;

        private void Nextbutton_Click(object sender, RoutedEventArgs e)
            int nindex = sample.SelectedIndex + 1;
            if (nindex >= sample.Items.Count)
                nindex = 0;
            sample.SelectedIndex = nindex;

        private void selectbutton_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("Selected Tab is:" + (sample.SelectedItem as TabItem).Header);

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google