Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Center TabItems in a TabControl

in my XAML code, I have a TabControl with multiple items. The problem I have is that I can not center the tabitems about the content area. The tabs are always starting on the left side, but I need them centered. This is my code:

<TabControl>
    <TabItem Header="Test 1"  Style="{StaticResource LeftTab}" Height="40" />
    <TabItem Header="Test 2"  Style="{StaticResource MiddleTab}"  />
    <TabItem Header="Test 3"  Style="{StaticResource MiddleTab}" />
    <TabItem Header="Test 4" Style="{StaticResource RightTab}"  />
</TabControl>

I do not know a property to center the items - any idea?

like image 811
Alexander Ilg Avatar asked Feb 16 '10 14:02

Alexander Ilg


1 Answers

Internally, the TabControl uses a TabPanel to layout the tabs. Using the default template, you just need to set the HorizontalAlignment of the TabPanel through a style:

<TabControl>
    <TabControl.Resources>
        <Style TargetType="{x:Type TabPanel}">
            <Setter Property="HorizontalAlignment" Value="Center" />
        </Style>
    </TabControl.Resources>

    <TabItem Header="Test 1" />
    <TabItem Header="Test 2" />
    <TabItem Header="Test 3" />
    <TabItem Header="Test 4" />
</TabControl>
like image 75
Heinzi Avatar answered Oct 21 '22 21:10

Heinzi