Skip to main content

Create Custom WPF Tab Control Easily


Windows Presentation Foundation (or WPF) is a computer-software graphical subsystem for rendering user interfaces in Windows-based applications. WPF, previously known as "Avalon", was initially released as part of .NET Framework 3.0. Rather than relying on the older GDI subsystem, WPF utilizes DirectX.

Creating a Tab Control in WPF is pretty simple. Let's get through the steps one by one.

Create a new Project. I am using VB as my language, you may C# if you wish to. :) A WPF Application Project will do fine.

I believe in clean coding. I wish to add a couple of controls to this solution. Instead of adding them to the root, I will right click the solution and click "Add Folder", rename as something like say "Cus Tab Controls".
Add a WPF custom control, name it whatever you wish. This will be the tabItem header. Here goes the code.

I just added a harmless Label, and a Button, which will act as the Close Tab button for the Tab Item.

<UserControl x:Class="cusTabHeader"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="25" d:DesignWidth="217" Name="cusTabHeader">
<Grid Name="cusTabHeaderMGrid">
<Button Height="18" Margin="0,2,3,0" Name="closeTabButton" VerticalAlignment="Top" Content="x" HorizontalAlignment="Right" Width="18" FontWeight="ExtraBold" />
<Label Content="Label" Height="26" HorizontalAlignment="Left" Margin="2,1,0,0" Name="tabTitleLabel" VerticalAlignment="Top" Width="151" />
</Grid>
</UserControl>
Now create a new Class as the Tab Item and inherit TabItem.

''' <summary>
''' Simple TabItem with close button
''' </summary>
''' <remarks>Just a scratch, nothing else!</remarks>

Public Class cusTabItem

Inherits TabItem
Sub New()
Dim _cusTabHeader As New cusTabHeader()
Me.Header = _cusTabHeader

'Add event handlers for the code
AddHandler _cusTabHeader.closeTabButton.MouseEnter, New MouseEventHandler(AddressOf button_close_MouseEnter)

AddHandler _cusTabHeader.closeTabButton.MouseLeave, New MouseEventHandler(AddressOf button_close_MouseLeave)

AddHandler _cusTabHeader.closeTabButton.Click, New RoutedEventHandler(AddressOf button_close_Click)

AddHandler _cusTabHeader.closeTabButton.SizeChanged, New SizeChangedEventHandler(AddressOf label_TabTitle_SizeChanged)

End Sub
Private title As String
Public Property setTabTitle() As String

Set(ByVal value As String)
title = value
TryCast(Me.Header, cusTabHeader).tabTitleLabel.Content = title
End Set
Get
Return title
End Get
End Property

'' Button MouseEnter - When the mouse is over the button - change color to Red

Sub button_close_MouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs)
TryCast(Me.Header, cusTabHeader).closeTabButton.Foreground = Brushes.Red
End Sub

'' Button MouseLeave - When mouse is no longer over button - change color back to black

Sub button_close_MouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs)
TryCast(Me.Header, cusTabHeader).closeTabButton.Foreground = Brushes.Black
End Sub

'' Button Close Click - Remove the Tab - (or raise an event indicating a "CloseTab" event has occurred)
Sub button_close_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
TryCast(Me.Parent, TabControl).Items.Remove(Me)
End Sub

'' Label SizeChanged - When the Size of the Label changes (due to setting the Title) set position of button properly
Sub label_TabTitle_SizeChanged(ByVal sender As Object, ByVal e As SizeChangedEventArgs)
TryCast(Me.Header, cusTabHeader).closeTabButton.Margin = _
New Thickness(TryCast(Me.Header, cusTabHeader).tabTitleLabel.ActualWidth + 5, 3, 4, 0)
End Sub

End Class

Compile the Project and add the Custom control you just created to a TabControl at runtime and you are set. :)

<tabcontrolName>.Items.Add(<nameofCustabItem>)

Comments

Popular posts from this blog

Stalingrad 2013 Movie Review

I saw the trailer of this movie once and really really liked it. What was really interesting was the Thomas Kretschmann acted in a movie with the same name exactly 20 years back. Stalingrad was released in 1993 and the young Kretschmann played the role of a dutiful but conscientious German leutnant of the ill fated 6th Armee. Here also he comes back, slightly older and rugged. The spectacular visuals, the background sound and the lack of background music makes it a realistic watch. That is all positive that I have to say about this trash. Yes, that's what I will this. Naturally, while watching this I brought in similar scenes of Stalingrad (1993) among other war movies. And in comparison this movies cuts so sorry a figure that I dared not watch the entire movie even! The story starts of slow enough and just completely refused to pick up pace. The entire movie seemed to be directed by an amateur school student, bolstered by some insanely talented special effects team. This made t...

Bangalore - Where I now live!

Life in Bangalore Bangalore or Bengaluru is one of the strangest places I have ever been. First, I'll call it Bangalore. In any case I am not really fond of this place, and the official name just makes me go nuts. Literally! Bangalore techie in action! Lets start with the good things in Bangalore. The advantage is that for me there ain't much good here so this paragraph is going to be short! Bangalore has excellent weather, inspite of not being the hills. Weather in Bangalore is always temperate, with the mercury hovering from 68 and 78 Fahrenheit. So yeah, unlike in other Indian metro cities, here we don't have to sweat. Electricity costs are also cut down because we don't need air conditioners or even fans. The other good thing about Bangalore is its vicinity to various tourist spots in South India. Be it the Nilgiris or the Malabar, Konkan or Goa, all are within reach and easily. Seriously??? Now lets begin the negative aspects of the city. I will ment...

Getting back to making stuff

I was off for a while. I mean, I was not. I painted an old shoecase that now looks a gorgeous white cupboard. I fixed my bicycle.  But beyond this, I did not do much beyond what my work paid me to do. So here I am, about to touch a new year of my life - and I am itching to get back into the habit of writing again.  Not just writing stuff here, but also making applications or music.  In fact, after a very long time I feel like sketching again. I think I am going to sketch my wife. Or may be finish my ex wife's sketch. I dunno. But I am gonna get started with a pencil tomorrow. Thanks to chatgpt, I have restarted my application brainstorming and dusted my old Github repos.  I think this is going to be a good run this time.