shaneker
shaneker

Reputation: 375

How do i make a WPF tab control's tab area smaller than the control?

I have a scenario where i have two buttons in the top right of my application, and i have a tab control that spans the entire application's screen. (Basically the two buttons are on the same horizontal line as the tabs in the tab control). The problem is that when i have multiple tabs open, the buttons and the tabs overlap. I don't want to have to specify the grid row/column numbers so that the buttons are above the tabs.

Is there any way to specify to the Tab control a certain area that it has to open tab controls before it automatically starts a second row of tabs?

In other words, if my tab control has a width of X, how can i tell the area that displays the actual tabs that it should only occupy say, x-15 amount of space.

Thanks!

Upvotes: 3

Views: 2681

Answers (1)

Rick Sladkey
Rick Sladkey

Reputation: 34240

You'll have to supply the ControlTemplate for the TabControl to do that. Here is an example where 100 pixels is reserved to the right of the header panel:

<Grid>
    <TabControl>
        <TabControl.Template>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid KeyboardNavigation.TabNavigation="Local"
                      SnapsToDevicePixels="true"
                      ClipToBounds="true">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition x:Name="ColumnDefinition0"/>
                        <ColumnDefinition x:Name="ColumnDefinition1"
                                          Width="100"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition x:Name="RowDefinition0"
                                       Height="Auto"/>
                        <RowDefinition x:Name="RowDefinition1"
                                       Height="*"/>
                    </Grid.RowDefinitions>
                    <TabPanel x:Name="HeaderPanel"
                              Panel.ZIndex ="1" 
                              KeyboardNavigation.TabIndex="1"
                              Grid.Column="0"
                              Grid.Row="0"
                              Margin="2,2,2,0"
                              IsItemsHost="true"/>
                    <Border x:Name="ContentPanel"
                            Background="{TemplateBinding Background}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            KeyboardNavigation.TabNavigation="Local"
                            KeyboardNavigation.DirectionalNavigation="Contained"
                            KeyboardNavigation.TabIndex="2"
                            Grid.Column="0"
                            Grid.Row="1"
                            Grid.ColumnSpan="2">
                        <ContentPresenter x:Name="PART_SelectedContentHost"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                          Margin="{TemplateBinding Padding}"
                                          ContentSource="SelectedContent"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </TabControl.Template>
        <TabItem Header="Item1"/>
        <TabItem Header="Item2"/>
        <TabItem Header="Item3"/>
        <TabItem Header="Item4"/>
    </TabControl>
</Grid>

Resize the window to see it in action.

Upvotes: 5

Related Questions