Click here to Skip to main content
6,291,124 members and growing! (16,349 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Beginner License: The Code Project Open License (CPOL)

Creating an Outlook Navigation Pane by Restyling a WPF TabControl

By rudigrobler

Restyling a standard TabControl to look like an Outlook Navigation Pane
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 3.0, .NET 3.5), XAML, WPF, Dev, Design
Posted:4 Mar 2008
Updated:24 Jun 2008
Views:46,208
Bookmarked:110 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
38 votes for this article.
Popularity: 6.78 Rating: 4.29 out of 5
1 vote, 2.6%
1
1 vote, 2.6%
2
3 votes, 7.9%
3
8 votes, 21.1%
4
25 votes, 65.8%
5

Introduction

Arguable Office 2007 has revolutionized the way modern user interfaces will look like with great innovations like the ribbon... The control I would like to focus on in this article though is the Office 2007 Navigation Pane, or more specifically, how to recreate the Navigation Pane by restyling a TabControl!

The WPF team has done an excellent job of reducing the need to create custom controls by allowing "lookless controls" to be re-styled! This is how the Navigation Pane looks:

OutlookPanel.jpg

And this is how our standard TabControl currently looks:

TabControl.jpg

Here is the XAML:

<TabControl VerticalAlignment="Stretch" Width="360" Height="Auto"
    TabStripPlacement="Bottom">
    <TabItem Header="Mail">
        <Grid/>
    </TabItem>
    <TabItem Header="Calendar">
        <Grid/>
    </TabItem>
    <TabItem Header="Contacts">
        <Grid/>
    </TabItem>
    <TabItem Header="Tasks">
        <Grid/>
    </TabItem>
</TabControl>

Before I begin restyling, to keep the beginner status for my article, I will briefly explain what a Style and Template are and how they are used here!

What is a Style?

A Style's main function is to group together property values that could otherwise be set individually. The intent is to then share this group of values among multiple elements. If you look at a simple Style in XAML, you will notice that generally it has a TargetType set and then a collection of Setters. Each Setter then allows you to replace a given Property value with the provided value. Here is a very basic example:

<Style x:Key="SimpleStyle" TargetType="{x:Type TabControl}">
    <Setter Property="Foreground" Value="Blue" />
    <Setter Property="Background" Value="Yellow" />
</Style>

While this is the standard definition of a Style, in this restyle article, it is more used to just replace the Template property.

What is a Template?

A Template allows you to completely replace an element’s visual tree with anything you can dream up, while keeping all of its functionality intact. Here is an example of how we use a style/setter to replace a Template.

<Style x:Key="SimpleStyle" TargetType="{x:Type TabControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <!-- The new visual tree should be placed here -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Since the Template is just a property on the Control object (from which TabControl derives), it can also be replaced by using a setter! We can now replace the complete visual tree while keeping the basic functionality of a TabControl!

For more information about deriving from Control, have a look here.

Restyling the TabControl

The TabControl uses a TabPanel to layout the TabControl's tabs (or buttons). We will now replace this with a StackPanel:

<Style x:Key="OutlookTabControlStyle" TargetType="{x:Type TabControl}">
    <Setter Property="Foreground"
        Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background"
        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
    <Setter Property="BorderBrush" Value=
      "{x:Static Microsoft_Windows_Themes:ClassicBorderDecorator.ClassicBorderBrush}"/>
    <Setter Property="BorderThickness" Value="3"/>
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="MinWidth" Value="10"/>
    <Setter Property="MinHeight" Value="10"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid ClipToBounds="true" SnapsToDevicePixels="true"
                      KeyboardNavigation.TabNavigation="Local">
                    <Grid.RowDefinitions>
                        <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                        <RowDefinition x:Name="RowDefinition1" Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition x:Name="ColumnDefinition0"/>
                        <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                    </Grid.ColumnDefinitions>
                    <Grid x:Name="ContentPanel" Grid.Column="0" Grid.Row="1"
                        KeyboardNavigation.DirectionalNavigation="Contained"
                        KeyboardNavigation.TabIndex="2"
                        KeyboardNavigation.TabNavigation="Local">
                        <Microsoft_Windows_Themes:ClassicBorderDecorator
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderStyle="Raised"
                        BorderThickness="{TemplateBinding BorderThickness}">
                            <ContentPresenter SnapsToDevicePixels=
                            "{TemplateBinding SnapsToDevicePixels}" Margin="2,2,2,2"
                            x:Name="PART_SelectedContentHost"
                            ContentSource="SelectedContent"/>
                        </Microsoft_Windows_Themes:ClassicBorderDecorator>
                    </Grid>
                    <StackPanel HorizontalAlignment="Stretch" Margin="0,-2,0,0"
                    x:Name="HeaderPanel" VerticalAlignment="Bottom" Width="Auto" 
                Height="Auto" Grid.Row="1" IsItemsHost="True"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="TabStripPlacement" Value="Bottom">
                        <Setter Property="Grid.Row"
                        TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Height"
                            TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height"
                            TargetName="RowDefinition1" Value="Auto"/>
                    </Trigger>
                    <Trigger Property="TabStripPlacement" Value="Left">
                        <Setter Property="Grid.Row"
                            TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Grid.Column"
                            TargetName="ContentPanel" Value="1"/>
                        <Setter Property="Width"
                            TargetName="ColumnDefinition0" Value="Auto"/>
                        <Setter Property="Width"
                            TargetName="ColumnDefinition1" Value="*"/>
                        <Setter Property="Height"
                            TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height"
                            TargetName="RowDefinition1" Value="0"/>
                    </Trigger>
                    <Trigger Property="TabStripPlacement" Value="Right">
                        <Setter Property="Grid.Row"
                            TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Grid.Column"
                            TargetName="ContentPanel" Value="0"/>
                        <Setter Property="Width"
                            TargetName="ColumnDefinition0" Value="*"/>
                        <Setter Property="Width"
                            TargetName="ColumnDefinition1" Value="Auto"/>
                        <Setter Property="Height"
                            TargetName="RowDefinition0" Value="*"/>
                        <Setter Property="Height"
                            TargetName="RowDefinition1" Value="0"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground"
                            Value="{DynamicResource
                            {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

All of this is automatically created by blend by right-clicking on the TabControl and selecting "Edit Control Parts (Template)" -> "Edit a Copy..."

BlendTemplateEditCopy.jpg

Next, in XAML just replace the TabPanel with the StackPanel. The only important property to set on the StackPanel is the IsItemsHost="true".

HeaderControl.jpg

After replacing the TabPanel with a StackPanel, set each TabItems height to 30, the TabControl's Background to White and the BorderBrush to #FF6593CF.

AfterApplyingBasicStyle.jpg

Well, this looks better...

What are Resources?

Resources or more specifically, ResourceDictionary is just a dictionary of key/value pairs that can be accessed from XAML. It is used here to define brushes that are commonly used. Here is a simple example:

<SolidColorBrush x:Key="OutlookButtonForeground" Color="#FF204D89"/>

and here a use for it:

Foreground="{DynamicResource OutlookButtonForeground}"

Restyling the TabItem

Next, create a style for a TabItem by right-clicking on one of them and selecting "Edit Control Parts (Template)" -> "Edit a Copy..."

Before I dig into the default style created, I just want to create two brush resources. The first brush resource is the normal color of a Navigation Pane button:

<LinearGradientBrush x:Key="OutlookButtonBackground" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FFD9EDFF" Offset="0"/>
    <GradientStop Color="#FFC0DEFF" Offset="0.445"/>
    <GradientStop Color="#FFC0D9GB" Offset="1"/>
    <GradientStop Color="#FFAFD1F8" Offset="0.53"/>
</LinearGradientBrush>

And the next is the default text color of a button:

<SolidColorBrush x:Key="OutlookButtonForeground" Color="#FF204D89"/>

So, each TabItem should now look something like this:

<TabItem Header="Tasks" Height="30" Style="{DynamicResource OutlookTabItemStyle}"
    Background="{DynamicResource OutlookButtonBackground}"
            Foreground="{an>DynamicResource OutlookButtonForeground}">
    <Grid/>
</TabItem>

Creating a style for the TabItem has two purposes: Align the TabItem's header to the left and change the TabItem's border from a ClassicBorder to a normal Border (for more control).

<Style x:Key="OutlookTabItemStyle" TargetType="{x:Type TabItem}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource TabItemFocusVisual}"/>
    <Setter Property="Padding" Value="12,2,12,2"/>
    <Setter Property="Foreground"
        Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background"
        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Border SnapsToDevicePixels="true" x:Name="Bd"
                    Background="{TemplateBinding Background}"
                    BorderThickness="1" BorderBrush="#FF6593CF">
                    <ContentPresenter SnapsToDevicePixels=
                        "{TemplateBinding SnapsToDevicePixels}"
                        Margin="{TemplateBinding Padding}"
                VerticalAlignment="{Binding Path=VerticalContentAlignment,
                RelativeSource={RelativeSource AncestorType=
                        {x:Type ItemsControl}}}"
                ContentSource="Header" RecognizesAccessKey="True"
                            HorizontalAlignment="Left"/>
                </Border>
                <ControlTemplate.Triggers>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This is how it looks now: "BetterStyle.jpg"

BetterStyle.jpg

Ahhh... even better! So, what's next?

What are Triggers?

Triggers have a collection of Setters just like Style (and/or collections of TriggerActions). But whereas a Style applies its values unconditionally, a trigger performs its work based on one or more conditions. There are three types of triggers:

  • Property triggers — Invoked when the value of a dependency property changes
  • Data triggers — Invoked when the value of a plain .NET property changes
  • Event triggers — Invoked when a routed event is raised

We will make use of an Event Trigger. The TabItem provides us with an IsSelected routed event!

Selection

We now need to indicate (by changing color) that an item is selected. Again, we create a brush to represent the color the button needs to change to once highlighted:

<LinearGradientBrush x:Key="OutlookButtonHighlight" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FFFFBD69" Offset="0"/>
    <GradientStop Color="#FFFFB75A" Offset="0.0967"/>
    <GradientStop Color="#FFFFB14C" Offset="0.2580"/>
    <GradientStop Color="#FFFB8C3C" Offset="0.3870"/>
    <GradientStop Color="#FFFEB461" Offset="0.9677"/>
    <GradientStop Color="#FFFEBB67" Offset="1"/>
</LinearGradientBrush>

And here is the trigger:

<ControlTemplate.Triggers>
    <Trigger Property="Selector.IsSelected" Value="True">
        <Setter Property="Background" TargetName="Bd"
                Value="{DynamicResource OutlookButtonHighlight}"/>
    </Trigger>
</ControlTemplate.Triggers>

Here is how it looks:

EvenBeter.jpg

That's better...

That is basically all that is required to restyle a TabControl!

History

  • 4th March, 2008 - Initial release
  • 5th March, 2008 - Changed the title, updated the colors
  • 23rd June, 2008 - Added source file

Please vote for this article if you liked it and also visit my blog.
Thank you!

License

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

About the Author

rudigrobler


Member

Location: South Africa South Africa

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
Generalgood Pingroupzhujinlong1984091321:45 16 Apr '09  
GeneralHow can i add picture and events.. Pinmemberfifiza3:46 10 Mar '09  
Generalhow can i provide shrink functonality in this control PinmemberOm S Pathak5:02 15 Dec '08  
GeneralNice Job... PinmemberCastle Rider19:16 16 Nov '08  
QuestionAbout the Images/Icons Pinmemberdearcyrix19:20 12 Oct '08  
NewsTabStripPlacement - does it work? PinmemberIndy200519:07 26 Jun '08  
GeneralRe: TabStripPlacement - does it work? Pinmemberrudigrobler21:10 26 Jun '08  
GeneralRe: TabStripPlacement - does it work? Pinmemberholy_spirit0:43 19 Jan '09  
AnswerRe: TabStripPlacement - does it work? Pinmemberholy_spirit2:19 19 Jan '09  
QuestionPlease help me PinmemberMember 268137223:32 18 Jun '08  
AnswerRe: Please help me Pinmemberrudigrobler0:51 23 Jun '08  
GeneralGood one Pinmemberpgr20063:16 16 May '08  
GeneralRe: Good one Pinmemberrudigrobler0:53 23 Jun '08  
GeneralNice Job! Pinmember BOTs - running in my mind 20:52 26 Mar '08  
GeneralRe: Nice Job! Pinmemberrudigrobler0:54 23 Jun '08  
GeneralNeato PinmvpSacha Barber9:59 6 Mar '08  
GeneralRe: Neato Pinmemberrudigrobler20:42 6 Mar '08  
GeneralOutlook brushes PinmemberBenoît Dion1:21 6 Mar '08  
GeneralNice PinmemberYogesh Jagota19:35 5 Mar '08  
GeneralRe: Nice PinmemberAngelo Cresta0:35 6 Mar '08  
GeneralBlend or VS2008 Pinmemberizwaldschwartz4:10 5 Mar '08  
GeneralRe: Blend or VS2008 Pinmemberrudigrobler4:17 5 Mar '08  
GeneralWonderfull work! PinmemberMeC++0:43 5 Mar '08  
GeneralRe: Wonderfull work! Pinmemberrudigrobler0:56 5 Mar '08  
GeneralSource Pinmemberdevnet24723:07 4 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jun 2008
Editor: Deeksha Shenoy
Copyright 2008 by rudigrobler
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project