Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabItem headers: Content doesn't stretch if the actual tab header does

Tags:

wpf

I have a TabControl and am attempting to get the headers to look nice. I have the following XAML:

(In Resources.xaml:)

<DataTemplate x:Key="ClosableTabItemTemplate">
    <DockPanel LastChildFill="True" MinWidth="200" Height="20" HorizontalAlignment="Stretch" behaviors:MouseClickBehavior.ClickCommand="{Binding Path=CloseCommand}">
        <Image Source="{Binding Path=Image}" Height="16" VerticalAlignment="Center" />
        <Button Background="Transparent"
                  BorderBrush="Transparent"
                  Command="{Binding Path=CloseCommand}"
                  Content="X"
                  Cursor="Hand"
                  DockPanel.Dock="Right"
                  Focusable="False" 
                  FontSize="9"
                  FontWeight="Bold"
                  Margin="3,0,0,0"
                  Padding="0"
                  VerticalAlignment="Center"
                  VerticalContentAlignment="Center"
                  Width="16" Height="16" />
        <TextBlock Text="{Binding Path=Header}" DockPanel.Dock="Left" VerticalAlignment="Center" />
    </DockPanel>
</DataTemplate>

(In MainWindow.xaml:)

<TabControl Grid.Column="1"
            ItemsSource="{Binding Path=Tabs}"
            ItemTemplate="{DynamicResource ClosableTabItemTemplate}" />

Here is a visual example of my problem:

I like the actual width of the tabs for now, but the fact that my dockpanel for the header isn't filling up all the space is bothersome. Same thing happens if I use a Grid, too (presumably any container control).

like image 848
Andrew Avatar asked Sep 21 '11 12:09

Andrew


People also ask

How do I set header text in the tabcontrol?

This section explains how to set header text and UI customization of the tab header in the TabControl. You can add a text for the each tab headers by using the TabItemExt.Header property.

How to edit the text of the tab header at runtime?

You can edit the text of the tab header at runtime by double clicking the tab header or selected a tab and pressing Ctrl + F2 key. You can restrict all the tab header editing by using the EnableLabelEdit property value as false. The default value of EnableLabelEdit property is true.

How to set the size of each tab in a tabitem?

You can set a size of the each tabs by using the TabItemExt.Width and TabItemExt.Height properties. You can also align the header content horizontally and vertically by using the TabItemExt.HorizontalContentAlignment and TabItemExt.VerticalContentAlignment properties.

How do I hide the header of a tab in HTML?

Hide tab header when there is single tab item You can hide the header of tab item only on when single tab item present in the TabControl. You can enable it by using the HideHeaderOnSingleChild property value as true. The Default value of HideHeaderOnSingleChild property is false.


1 Answers

ContentPresenter.HorizontalAlignment in the default style for TabItem is bound to ItemsControl.HorizontalContentAlignment. Here it is, taken from Aero.NormalColor.xaml:

<ContentPresenter Name="Content"
                  ContentSource="Header"
                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                  HorizontalAlignment="{Binding Path=HorizontalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                  VerticalAlignment="{Binding Path=VerticalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                  RecognizesAccessKey="True"/>

Setting TabControl.HorizontalContentAlignment to Stretch fixes the issue for me.

<TabControl HorizontalContentAlignment="Stretch" />
like image 162
Alex Humphrey Avatar answered Nov 13 '22 16:11

Alex Humphrey