C# Exercises

Home AgriMetSoft About Contact

How to use TabControl in C# WPF - Wrap Header and Icon Header

	    
//==========================xaml file===========================================
<Window x:Class="WPF_TabControl.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPF_TabControl"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
  <Grid>
    <TabControl Name="tab1" SelectionChanged="tab1_SelectionChanged">
      <TabItem Header="First">
        <Image Source="/img/icon.ico" Margin="100"/>
      </TabItem>
      <TabItem>
        <TabItem.Header>
          <TextBlock Margin="3" Width="100" Text="Second Tab of Tab Control" TextAlignment="Center" TextWrapping="Wrap"/>
        </TabItem.Header>
        <StackPanel>
          <Button Width="100" Content="Button" Margin="30"/>
        </StackPanel>
      </TabItem>
      <TabItem>
        <TabItem.Header>
          <StackPanel Orientation="Horizontal">
            <Image Source="/img/icon.ico" Width="30"/>
            <TextBlock Margin="3" Text="Third" TextAlignment="Center"/>
          </StackPanel>
        </TabItem.Header>
        <StackPanel>
          <Button Width="100" Content="Icon Header" Margin="30"/>
        </StackPanel>
      </TabItem>
    </TabControl>
  </Grid>
</Window>

///==============================cs file=====================================
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.Navigation;
using System.Windows.Shapes;

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

    private void tab1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (e.Source is TabControl) //if this event fired from TabControl then enter
      {
        if (tab1.SelectedIndex == 0)
        {
          var a = 1;
        }
      }
    }
  }
}

		
	 


Download the project of Visual Studio 2022 in DropBox Download


WPF C# TabControl - Wrap Header and Icon Header - SelectionChanged Event


List of Exercises