Click here to Skip to main content
Click here to Skip to main content

An Extended WPF TabControl

, 5 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Altering the WPF TabControl to show a single row of scrolling TabItems
Screenshot - TabControl_Default.jpg


I recently decided to have a go at changing the built-in WPF TabControl so that it behaves similar to the way in which Internet Explorer 7 works.

The main area I wanted to change was the way in which the TabItems are arranged, currently the built-in TabControl will pack and stack items, so the more items there are, the more room is required for the headers, taking up space from the actual content of the TabItem.

With Internet Explorer 7, tabs remain on a single row and will scroll in and out of view. This is the way I wanted this control to behave.


There are a few extra properties (Dependency Properties) and events added to the control.

  • AllowAddNew - Setting this property to true will allow the end user to add new tabitems to the control via a button.
  • AllowDelete - Setting this property to true will allow the end user to delete tabitems from the control by clicking on the close button on the selected tabitem.

If the above properties are set to true, there are three events that go with these properties. They are TabItemAdded, TabItemClosing and TabItemClosed: all provide a reference to the TabItem being added/removed, the TabItemClosing event providing a place to cancel the close if required.

  • AddNewTabToEnd - If this property is true, tabitems added using the AddNew button will be added to the end of the row of tabitems, otherwise the new tabitem will be inserted immediately after the current selection.
  • SelectNewTabOnCreate - If this property is true, focus will be moved to the new tabitem when added.

Finally, there are three extra properties of type Brush which are used for coloring the tabitems based on their state:

  • TabItemNormalBackground
  • TabItemMouseOverBackground
  • TabItemSelectedBackground

If you look at the picture at the top of this article, it shows the default style for the TabControl. I've intentionally made this very basic. By just using the extra Background properties, you can easily change the appearance of the TabControl. As this picture shows, by adding a few LinearGradientBrushes, you can get something that resembles Internet Explorer 7.


There are four properties which take an active part in the arrangement of the tabitems. They are:

  • TabItemMinWidth
  • TabItemMaxWidth
  • TabItemMinHeight
  • TabItemMaxHeight

Arranging the TabItems

There are three ways in which the tabcontrol arranges its tabitems.
The following description is relevant if the TabStripPlacement property is set to Top or Bottom.

  1. If the combined widths of the TabItems is less than the available visible space, then we just arrange them one after the other, taking into account the TabItemMaxWidth and TabItemMinWidth properties.
  2. If the combined widths are greater than visible, then calculate the width required for all the tabitems so that all are visible. If this width is greater than the TabItemMinWidth property, then we arrange the tabitems using this calculated width.
  3. Once we add too many tabitems that their widths would be less than the TabitemMinWidth property, then scrolling will be enabled.

The class that is responsible for arranging the tabitems is the VirtualizingTabPanel. As the name suggests, this panel derives from the VirtualizingPanel class and implements the IScrollInfo interface. I won't describe here what a VirtualizingPanel is, instead I will just direct you to this Blog post by Dan Crevier which describes what it is and how to implement one.

Using the Code

To use the control, you will need to add a reference to the Wpf.TabControl assembly, then add an xmlns to the window:


The demo provided shows how to add the control to a window and set its properties.


  • 13-Oct-2007: Initial upload
  • 09-Mar-2009: Implemented ItemsSource binding & bug fixes
  • 05-Apr-2009: Updated source code


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

GeneralBlink tab item in changes. PinmemberMorteza_Ar27-Mar-09 3:26 
QuestionExtended Silverlight TabControl PinmemberMohammed Issa9-Mar-09 4:59 
Generaldata binding on ItemsSource PinmemberRavejones25-Feb-09 7:22 
GeneralRe: data binding on ItemsSource Pinmemberalrh11-Mar-09 2:52 
Generalmodification: do not virtualize selected tab Pinmemberchefkokkie14-Feb-09 20:07 
GeneralWPF Chrome causes this to not work. PinmemberMember 44541963-Feb-09 15:13 
AnswerRe: WPF Chrome causes this to not work. [modified] Pinmemberchefkokkie11-Feb-09 21:42 
Answernon-uniform width decrease of tabs Pinmemberchefkokkie3-Feb-09 11:27 
Instead of decreasing the width of tabs uniformly I prefer the widest tab to decrease first until it is equal in size to the next widest tab at which point both decrease further, etc...
To accomplish the same, replace everything between // 2) and // 3) in the MeasureHorizontal method in VirtualizationTabPanel.cs with:
// 2). all the children fit in the available space if we reduce their widths starting with the widest tabs
// while staying within the MinimumChildWidth and MaximumChildWidth constraints
if (!flag)
// make sure the width is not greater than the MaximumChildWidth constraints
double requiredWidth = Math.Min(MaximumChildWidth, availableSize.Width / _actualChildCount);
// target width applies now if whether we can fit all items in the available space or whether we are scrolling
if (requiredWidth >= MinimumChildWidth)
_maxVisibleItems = ic.Items.Count;
FirstVisibleIndex = 0;
extentWidth = 0;
double left = 0;
double targetWidth = 0;
double[] sortedWidths = new double[_actualChildCount];
for (int i = 0; i < _actualChildCount; i++)
sortedWidths[i] = widths[i];
double remainingWidth = availableSize.Width;
for (int i = 0; i < _actualChildCount; i++)
if ((_actualChildCount - i) * sortedWidths[i] > remainingWidth )
targetWidth = remainingWidth / (_actualChildCount-i);
remainingWidth -= sortedWidths[i];
for (int i = 0; i < _actualChildCount; i++)
if (widths[i] > targetWidth) widths[i] = targetWidth;
extentWidth += widths[i];
_childRects[i] = new Rect(left, 0, widths[i], _maxChildWidthOrHeight);
left += widths[i];
_extent = new Size(extentWidth, _maxChildWidthOrHeight);
flag = true;
CanScrollLeftOrUp = false;
CanScrollRightOrDown = false;
Additionally, I found the resizing to max width when not all tabs fit to be "jumpy". I prefer to let them increase in size to their desired size, rather than the larger uniform targetWidth, and leave a little empty space on the right.
Within the // 3) section you will need to replace:
extentWidth += targetWidth;
widths[i] = targetWidth;
if (widths[i] > targetWidth) widths[i] = targetWidth;
extentWidth += widths[i];
I did not change the MeasureVertical method since I do not use left or right tabstrip placement. Naturally, you could duplicate the above if you do.
AnswerRepeatLEFT and RepeatRIGHT are reversed Pinmemberchefkokkie2-Feb-09 17:24 
GeneralRe: RepeatLEFT and RepeatRIGHT are reversed PinmemberRolenun24-May-09 19:12 
GeneralWow PinmemberAlomgir Miah Abdul26-Dec-08 7:55 
GeneralScrollViewer property - VerticalScrollBarVisibility="Auto" error Pinmemberbluemoong30-Oct-08 5:30 
GeneralVB Translation PinmemberRiccardoG6-Oct-08 6:16 
GeneralSet Window1's Height=Auto And Width=Auto ,there is an Error! Pinmemberanqincmt7-Jul-08 1:31 
GeneralRe: Set Window1's Height=Auto And Width=Auto ,there is an Error! Pinmemberjutifruti11-Sep-08 5:57 
GeneralRe: Set Window1's Height=Auto And Width=Auto ,there is an Error! PinmemberCool Cassis1-Oct-08 2:13 
QuestionAutoSize HeaderWidth of TabItem PinmemberHorst Klein17-Jun-08 3:26 
QuestionCan be used in XBAP apps? Pinmemberpixel3cs9-May-08 11:17 
Generalimplement data binding feature PinmemberQuang Tran Minh16-Apr-08 1:20 
GeneralRe: implement data binding feature Pinmembericecreman18-Apr-08 5:12 
GeneralRe: implement data binding feature PinmemberQuang Tran Minh22-Apr-08 3:04 
GeneralRe: implement data binding feature PinmemberDividian28-Oct-08 13:32 
Generaldatabinding PinmemberQuang Tran Minh15-Apr-08 22:42 
GeneralDatabinding? [modified] Pinmember88Keys10-Dec-07 13:25 
GeneralRe: Databinding? PinmemberRootSystem19-Jun-08 9:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 5 Apr 2009
Article Copyright 2007 by alrh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid