TabControl
A tab control with sixteen built-in tab styles, closable tabs, an add-new-tab button, a strip that scrolls when the tabs overflow, and every colour open to change – for document windows, browsers, editors and settings pages.
public class TabControl : System.Windows.Controls.TabControl
Inherits from / implements: System.Windows.Controls.TabControl
Remarks
It is a TabControl, so selection, keyboard navigation,
TabStripPlacement (top, bottom, left or right) and
SelectionChanged all work as usual.
Fill it with this assembly's TabItem, which adds the close button; the tab styles and
colours apply to those tabs.
Look
RenderingMode chooses the tab style – the classic shaped tabs
(TabControlRenderingMode.Default, TabControlRenderingMode.Trapezium,
TabControlRenderingMode.Diamond ...) or the flatter modern ones
(TabControlRenderingMode.Browser, TabControlRenderingMode.Fluent,
TabControlRenderingMode.Underline ...) – and can be changed at any time without
moving the selection. With TabControlRenderingMode.Custom, draw the tabs with a
template of your own in TabItemControlTemplate.
Every colour has a property: the page (TabControlBackground), the tabs
(TabItemPathBrush, TabItemSelectedFill,
TabItemTextForeground), the accent of the modern modes
(TabItemAccentBrush), the strip, the scroll buttons and the close buttons. The
defaults come from the keys in SharedResources.TabControlResources: with no Binarymission
skin in force they are the control's own warm orange and gold scheme, and with a skin they follow
the skin. A brush set on the control wins over both.
Opening and closing tabs
The control never adds or removes tabs itself, so the application stays in charge of its
documents. IsAddNewTabItemButtonVisible shows a "+" button at the end of the strip
that raises AddNewTabItemClick. A tab's close button, a middle click on its header and
Ctrl+F4 on the selected tab raise TabItem.CloseTabItemClick, where the application
can ask to save changes and then remove the tab.
Many tabs
When the tabs do not fit, the strip scrolls, with a pair of arrow buttons at its end to move along it.
Examples
A document window with browser-style tabs, an add button, and tabs that close when asked.
<Window x:Class="Notes.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tabs="clr-namespace:Binarymission.WPF.Controls.Containers;assembly=Binarymission.WPF.Controls.TabControl"
Title="Notes" Width="900" Height="600">
<tabs:TabControl x:Name="Documents"
RenderingMode="Browser"
TabStripBackground="#E8EBEF"
TabControlBackground="White"
IsAddNewTabItemButtonVisible="True"
AddNewTabItemTooltipText="New note"
CloseTabTooltipText="Close note"
AddNewTabItemClick="OnAddNote"
tabs:TabItem.CloseTabItemClick="OnCloseNote">
<tabs:TabItem Header="Welcome" IsCloseButtonVisible="False">
<TextBlock Margin="16" Text="Press + to start a new note." />
</tabs:TabItem>
<tabs:TabItem Header="Meeting notes">
<TextBox AcceptsReturn="True" />
</tabs:TabItem>
</tabs:TabControl>
</Window>The handlers that add and remove the tabs.
using System.Windows;
using System.Windows.Controls;
using Binarymission.WPF.Controls.Containers;
using TabItem = Binarymission.WPF.Controls.Containers.TabItem;
private int _untitled = 1;
private void OnAddNote(object sender, RoutedEventArgs e)
{
TabItem note = new TabItem
{
Header = "Untitled " + _untitled++,
Content = new TextBox { AcceptsReturn = true }
};
Documents.Items.Add(note);
Documents.SelectedItem = note;
}
private void OnCloseNote(object sender, RoutedEventArgs e)
{
TabItem note = (TabItem)e.OriginalSource;
if (MessageBox.Show("Close " + note.Header + "?", "Notes", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Documents.Items.Remove(note);
}
}See also
Members at a glance
| Properties | |
|---|---|
AddNewItemButtonBackground | Gets or sets the background of the add-new-tab button. |
AddNewItemContentBrush | Gets or sets the colour of the plus sign on the add-new-tab button. |
AddNewTabItemTooltipText | Gets or sets the tooltip shown over the add-new-tab button. |
ButtonLeftArrowGeometryPathFillBrush | Gets or sets the colour of the arrow on the button that scrolls the tab strip back. |
ButtonLeftArrowGeometryPathStrokeBrush | Gets or sets the colour of the outline of the arrow on the button that scrolls the tab strip back. |
ButtonRightArrowGeometryPathFillBrush | Gets or sets the colour of the arrow on the button that scrolls the tab strip on. |
ButtonRightArrowGeometryPathStrokeBrush | Gets or sets the colour of the outline of the arrow on the button that scrolls the tab strip on. |
CloseButtonCrossMarkColorBrush | Gets or sets the colour of the cross on each tab's close button. |
CloseButtonStrokeBrush | Gets or sets the colour of the thin outline round the cross on each tab's close button. |
CloseTabTooltipText | Gets or sets the tooltip shown over each tab's close button. |
IsAddNewTabItemButtonVisible | Gets or sets whether a "+" button is shown at the end of the tab strip for the user to open a new tab. |
RenderingMode | Gets or sets the shape and style the tabs are drawn in. |
ScrollButtonBrush | Gets or sets the background of the two buttons that scroll the tab strip when the tabs do not fit. |
ScrollButtonOnMouseOverBrush | Gets or sets the background of a scroll button while the pointer is over it. |
TabControlBackground | Gets or sets the background of the page area, where the selected tab's content is shown. |
TabItemAccentBrush | Gets or sets the accent colour the modern rendering modes mark the selected tab with: the filled pill, the underline, the selection bar, the outline. |
TabItemAccentForeground | Gets or sets the colour of text drawn on TabItemAccentBrush, such as the selected
tab's header in the TabControlRenderingMode.Pill,
TabControlRenderingMode.Backstage and TabControlRenderingMode.Glass modes. |
TabItemBorderBrush | Gets or sets the colour of the outline round the tabs and the page area. |
TabItemBorderOnMouseOverBrush | Gets or sets the colour of the outline of a tab while the pointer is over it, in the classic rendering modes. |
TabItemCloseButtonFillOnMouseOverBrush | Gets or sets the colour of the cross on a tab's close button while the pointer is over it. |
TabItemControlTemplate | Gets or sets the template every tab is drawn with. |
TabItemFillOnMouseOverBrush | Gets or sets the fill of a tab that is not selected while the pointer is over it, in the classic rendering modes. |
TabItemPathBrush | Gets or sets the fill of the tabs that are not selected, in the classic rendering modes. |
TabItemSelectedFill | Gets or sets the fill of the selected tab in the classic rendering modes. |
TabItemTextForeground | Gets or sets the colour of the tabs' header text. |
TabStripBackground | Gets or sets the colour behind the tab headers, including the scroll and add buttons. |
| Methods | |
OnKeyDown | Asks for the selected tab to be closed when the user presses Ctrl+F4, as in other Windows applications, and otherwise handles the key as a tab control does. |
| Events | |
AddNewTabItemClick | Occurs when the user clicks the "+" button at the end of the tab strip. |
| Fields | |
AddNewItemButtonBackgroundProperty | Identifies the AddNewItemButtonBackground dependency property. |
AddNewItemContentBrushProperty | Identifies the AddNewItemContentBrush dependency property. |
AddNewTabItemClickEvent | Identifies the AddNewTabItemClick routed event. |
AddNewTabItemTooltipTextProperty | Identifies the AddNewTabItemTooltipText dependency property. |
ButtonLeftArrowGeometryPathFillBrushProperty | Identifies the ButtonLeftArrowGeometryPathFillBrush dependency property. |
ButtonLeftArrowGeometryPathStrokeBrushProperty | Identifies the ButtonLeftArrowGeometryPathStrokeBrush dependency property. |
ButtonRightArrowGeometryPathFillBrushProperty | Identifies the ButtonRightArrowGeometryPathFillBrush dependency property. |
ButtonRightArrowGeometryPathStrokeBrushProperty | Identifies the ButtonRightArrowGeometryPathStrokeBrush dependency property. |
CloseButtonCrossMarkColorBrushProperty | Identifies the CloseButtonCrossMarkColorBrush dependency property. |
CloseButtonStrokeBrushProperty | Identifies the CloseButtonStrokeBrush dependency property. |
CloseTabTooltipTextProperty | Identifies the CloseTabTooltipText dependency property. |
IsAddNewTabItemButtonVisibleProperty | Identifies the IsAddNewTabItemButtonVisible dependency property. |
RenderingModeProperty | Identifies the RenderingMode dependency property. |
ScrollButtonBrushProperty | Identifies the ScrollButtonBrush dependency property. |
ScrollButtonOnMouseOverBrushProperty | Identifies the ScrollButtonOnMouseOverBrush dependency property. |
TabControlBackgroundProperty | Identifies the TabControlBackground dependency property. |
TabItemAccentBrushProperty | Identifies the TabItemAccentBrush dependency property. |
TabItemAccentForegroundProperty | Identifies the TabItemAccentForeground dependency property. |
TabItemBorderBrushProperty | Identifies the TabItemBorderBrush dependency property. |
TabItemBorderOnMouseOverBrushProperty | Identifies the TabItemBorderOnMouseOverBrush dependency property. |
TabItemCloseButtonFillOnMouseOverBrushProperty | Identifies the TabItemCloseButtonFillOnMouseOverBrush dependency property. |
TabItemControlTemplateProperty | Identifies the TabItemControlTemplate dependency property. |
TabItemFillOnMouseOverBrushProperty | Identifies the TabItemFillOnMouseOverBrush dependency property. |
TabItemPathBrushProperty | Identifies the TabItemPathBrush dependency property. |
TabItemSelectedFillProperty | Identifies the TabItemSelectedFill dependency property. |
TabItemTextForegroundProperty | Identifies the TabItemTextForeground dependency property. |
TabStripBackgroundProperty | Identifies the TabStripBackground dependency property. |
Properties
AddNewItemButtonBackground
Gets or sets the background of the add-new-tab button.
- Value
- The default is moccasin, from
TabControlResources.AddNewItemButtonBackgroundBrushKey.
See also
AddNewItemContentBrush
Gets or sets the colour of the plus sign on the add-new-tab button.
- Value
- The default is black, from
TabControlResources.AddNewItemContentBrushKey.
See also
AddNewTabItemTooltipText
Gets or sets the tooltip shown over the add-new-tab button.
- Value
- The default style sets
"Add New Tab item"; the value registered on the property isnull, which shows no tooltip.
See also
ButtonLeftArrowGeometryPathFillBrush
Gets or sets the colour of the arrow on the button that scrolls the tab strip back.
- Value
- The default is black, from
TabControlResources.ButtonLeftArrowGeometryPathFillBrushKey.
Remarks
The scroll buttons are enabled only when there are tabs out of sight in their direction.
See also
ButtonLeftArrowGeometryPathStrokeBrush
Gets or sets the colour of the outline of the arrow on the button that scrolls the tab strip back.
- Value
- The default is black, from
TabControlResources.ButtonLeftArrowGeometryPathStrokeBrushKey.
See also
ButtonRightArrowGeometryPathFillBrush
Gets or sets the colour of the arrow on the button that scrolls the tab strip on.
- Value
- The default is black, from
TabControlResources.ButtonRightArrowGeometryPathFillBrushKey.
See also
ButtonRightArrowGeometryPathStrokeBrush
Gets or sets the colour of the outline of the arrow on the button that scrolls the tab strip on.
- Value
- The default is black, from
TabControlResources.ButtonRightArrowGeometryPathStrokeBrushKey.
See also
CloseButtonCrossMarkColorBrush
Gets or sets the colour of the cross on each tab's close button.
- Value
- The default is maroon, from
TabControlResources.CloseButtonCrossMarkColorBrushKey.
Remarks
Under the pointer the cross takes TabItemCloseButtonFillOnMouseOverBrush
instead.
See also
CloseButtonStrokeBrush
Gets or sets the colour of the thin outline round the cross on each tab's close button.
- Value
- The default is dark grey (
#333333), fromTabControlResources.CloseButtonStrokeBrushKey.
See also
CloseTabTooltipText
Gets or sets the tooltip shown over each tab's close button.
- Value
- The default style sets
"Close this Tab page"; the value registered on the property isnull, which shows no tooltip.
Remarks
Set it to text in the application's own language and terms – "Close document", "Close customer".
See also
IsAddNewTabItemButtonVisible
Gets or sets whether a "+" button is shown at the end of the tab strip for the user to open a new tab.
- Value
trueto show the button. The default isfalse.
Remarks
The button sits after the scroll buttons, is drawn with AddNewItemButtonBackground and
AddNewItemContentBrush, shows AddNewTabItemTooltipText, and raises
AddNewTabItemClick – handle that to add the tab.
See also
RenderingMode
Gets or sets the shape and style the tabs are drawn in.
- Value
- One of the
TabControlRenderingModevalues. The default isTabControlRenderingMode.Default.
Remarks
It can be changed at any time, for example from a user preference, and the selected tab stays
selected. Each mode works with every TabStripPlacement;
on the left and right, the shaped modes draw their side-strip equivalent.
The classic modes are coloured by TabItemPathBrush,
TabItemSelectedFill, TabItemFillOnMouseOverBrush and
TabItemBorderOnMouseOverBrush; the modern modes by TabItemAccentBrush,
TabItemAccentForeground and TabControlBackground.
Example
<tabs:TabControl RenderingMode="Fluent" TabItemAccentBrush="#0F6CBD" TabStripPlacement="Left" />See also
ScrollButtonBrush
Gets or sets the background of the two buttons that scroll the tab strip when the tabs do not fit.
- Value
- The default is a vertical gradient from orange to cream, from
TabControlResources.ScrollButtonBrushKey.
Remarks
A disabled scroll button – one with no more tabs to show in its direction – is drawn at 40% opacity.
See also
ScrollButtonOnMouseOverBrush
Gets or sets the background of a scroll button while the pointer is over it.
- Value
- The default is a horizontal gradient from orange to cream, from
TabControlResources.ScrollButtonOnMouseOverBrushKey.
See also
TabControlBackground
Gets or sets the background of the page area, where the selected tab's content is shown.
- Value
- The default is a vertical gradient from white to moccasin, from
TabControlResources.TabControlBackgroundBrushKey.
Remarks
The TabControlRenderingMode.Browser, TabControlRenderingMode.Card
and TabControlRenderingMode.RoundedTop modes also fill the selected tab with it, so
the tab and its page read as one piece.
See also
TabItemAccentBrush
Gets or sets the accent colour the modern rendering modes mark the selected tab with: the filled pill, the underline, the selection bar, the outline.
- Value
- The default is blue (
#2B6CD4), fromTabControlResources.TabItemAccentBrushKey.
Remarks
Used by TabControlRenderingMode.Pill, TabControlRenderingMode.Underline,
TabControlRenderingMode.Fluent, TabControlRenderingMode.Backstage,
TabControlRenderingMode.Outline, TabControlRenderingMode.Glass and
TabControlRenderingMode.Minimal, and by the side-strip tabs. Usually the
application's brand or accent colour.
See also
TabItemAccentForeground
Gets or sets the colour of text drawn on TabItemAccentBrush, such as the selected
tab's header in the TabControlRenderingMode.Pill,
TabControlRenderingMode.Backstage and TabControlRenderingMode.Glass modes.
- Value
- The default is white, from
TabControlResources.TabItemAccentForegroundBrushKey.
TabItemBorderBrush
Gets or sets the colour of the outline round the tabs and the page area.
- Value
- The default is white, from
TabControlResources.TabItemBorderBrushKey.
See also
TabItemBorderOnMouseOverBrush
Gets or sets the colour of the outline of a tab while the pointer is over it, in the classic rendering modes.
- Value
- The default is white, from
TabControlResources.TabItemBorderOnMouseOverBrushKey.
See also
TabItemCloseButtonFillOnMouseOverBrush
Gets or sets the colour of the cross on a tab's close button while the pointer is over it.
- Value
- The default is red, from
TabControlResources.TabItemCloseButtonFillOnMouseOverBrushKey.
See also
TabItemControlTemplate
Gets or sets the template every tab is drawn with.
- Value
- The default style and
RenderingModeset it to the built-in template for the mode and the strip placement. Its registered value isnull.
Remarks
Set RenderingMode to TabControlRenderingMode.Custom and give this a
template of your own, targeting TabItem, to draw tabs that none of the built-in
modes offer. A template you set yourself takes precedence over the one the rendering mode chooses.
Name a Button in it PART_TabItemCloseButton for the tab to have a working
close button.
Example
<tabs:TabControl RenderingMode="Custom">
<tabs:TabControl.TabItemControlTemplate>
<ControlTemplate TargetType="tabs:TabItem">
<Border x:Name="Chrome" Padding="12,6" Background="Transparent">
<StackPanel Orientation="Horizontal">
<ContentPresenter ContentSource="Header" VerticalAlignment="Center" />
<Button x:Name="PART_TabItemCloseButton" Content="x" Margin="8,0,0,0" Padding="4,0" />
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Chrome" Property="Background" Value="#DCE6F4" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</tabs:TabControl.TabItemControlTemplate>
</tabs:TabControl>TabItemFillOnMouseOverBrush
Gets or sets the fill of a tab that is not selected while the pointer is over it, in the classic rendering modes.
- Value
- The default is a vertical gradient from tomato to white, from
TabControlResources.TabItemFillOnMouseOverBrushKey.
See also
TabItemPathBrush
Gets or sets the fill of the tabs that are not selected, in the classic rendering modes.
- Value
- The default is a vertical gradient from gold to white, from
TabControlResources.TabItemPathBrushKey.
Remarks
The fill of the tab's shape in TabControlRenderingMode.Default,
TabControlRenderingMode.Trapezium, TabControlRenderingMode.RaisedTrapezium,
TabControlRenderingMode.Diamond and the side-strip tabs. Under the pointer it gives way
to TabItemFillOnMouseOverBrush, and on the selected tab to
TabItemSelectedFill.
TabItemSelectedFill
Gets or sets the fill of the selected tab in the classic rendering modes.
- Value
- The default is a vertical gradient from orange (
#FD8E05) to cream, fromTabControlResources.TabItemSelectedFillBrushKey.
Remarks
To make the selected tab look joined to its page, give it the same colour as
TabControlBackground.
See also
TabItemTextForeground
Gets or sets the colour of the tabs' header text.
- Value
- The default is black, from
TabControlResources.TabItemTextForegroundBrushKey.
Remarks
Some modes override it for particular states – the selected tab in the accent-filled
modes uses TabItemAccentForeground.
TabStripBackground
Gets or sets the colour behind the tab headers, including the scroll and add buttons.
- Value
- The default is
null: the strip is transparent and whatever is behind the control shows through. A Binarymission skin, or the application definingTabControlResources.TabStripBackgroundBrushKey, sets one.
Remarks
Set it when the control sits on a dark or busy background: unselected tabs in the flat modes draw no fill of their own, so their text is read against what is behind the strip.
See also
Methods
OnKeyDown
Asks for the selected tab to be closed when the user presses Ctrl+F4, as in other Windows applications, and otherwise handles the key as a tab control does.
Parameters
e- The key pressed.
Remarks
Ctrl+F4 raises TabItem.CloseTabItemClick on the selected tab when it is a
TabItem that is enabled and shows its close button; otherwise the key is passed
on.
Events
AddNewTabItemClick
Occurs when the user clicks the "+" button at the end of the tab strip.
Remarks
The button is shown while IsAddNewTabItemButtonVisible is on. The control does
not add a tab itself: add a TabItem to ItemsControl.Items in the handler,
or an item to the bound collection, and select it if the user should start on it.
A bubbling routed event, so a parent element can handle it for several tab controls.
Example
documents.AddNewTabItemClick += (sender, e) =>
{
TabItem page = new TabItem { Header = "New query", Content = new QueryEditor() };
documents.Items.Add(page);
documents.SelectedItem = page;
};See also
Fields
AddNewItemButtonBackgroundProperty static
Identifies the AddNewItemButtonBackground dependency property.
AddNewItemContentBrushProperty static
Identifies the AddNewItemContentBrush dependency property.
AddNewTabItemClickEvent static
Identifies the AddNewTabItemClick routed event.
AddNewTabItemTooltipTextProperty static
Identifies the AddNewTabItemTooltipText dependency property.
ButtonLeftArrowGeometryPathFillBrushProperty static
Identifies the ButtonLeftArrowGeometryPathFillBrush dependency property.
ButtonLeftArrowGeometryPathStrokeBrushProperty static
Identifies the ButtonLeftArrowGeometryPathStrokeBrush dependency property.
ButtonRightArrowGeometryPathFillBrushProperty static
Identifies the ButtonRightArrowGeometryPathFillBrush dependency property.
ButtonRightArrowGeometryPathStrokeBrushProperty static
Identifies the ButtonRightArrowGeometryPathStrokeBrush dependency property.
CloseButtonCrossMarkColorBrushProperty static
Identifies the CloseButtonCrossMarkColorBrush dependency property.
CloseButtonStrokeBrushProperty static
Identifies the CloseButtonStrokeBrush dependency property.
CloseTabTooltipTextProperty static
Identifies the CloseTabTooltipText dependency property.
IsAddNewTabItemButtonVisibleProperty static
Identifies the IsAddNewTabItemButtonVisible dependency property.
RenderingModeProperty static
Identifies the RenderingMode dependency property.
ScrollButtonBrushProperty static
Identifies the ScrollButtonBrush dependency property.
ScrollButtonOnMouseOverBrushProperty static
Identifies the ScrollButtonOnMouseOverBrush dependency property.
TabControlBackgroundProperty static
Identifies the TabControlBackground dependency property.
TabItemAccentBrushProperty static
Identifies the TabItemAccentBrush dependency property.
TabItemAccentForegroundProperty static
Identifies the TabItemAccentForeground dependency property.
TabItemBorderBrushProperty static
Identifies the TabItemBorderBrush dependency property.
TabItemBorderOnMouseOverBrushProperty static
Identifies the TabItemBorderOnMouseOverBrush dependency property.
TabItemCloseButtonFillOnMouseOverBrushProperty static
Identifies the TabItemCloseButtonFillOnMouseOverBrush dependency property.
TabItemControlTemplateProperty static
Identifies the TabItemControlTemplate dependency property.
TabItemFillOnMouseOverBrushProperty static
Identifies the TabItemFillOnMouseOverBrush dependency property.
TabItemPathBrushProperty static
Identifies the TabItemPathBrush dependency property.
TabItemSelectedFillProperty static
Identifies the TabItemSelectedFill dependency property.
TabItemTextForegroundProperty static
Identifies the TabItemTextForeground dependency property.
TabStripBackgroundProperty static
Identifies the TabStripBackground dependency property.