Ask Your Question
3

How can I display text under tabs in XAML?

asked 2022-06-01 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-04-09 01:00:00 +0000

plato gravatar image

You can display text under tabs in XAML by using the TabControl and adding a DataTemplate with a TabItem Header and Content.

Here's an example:

<TabControl>
    <TabControl.Resources>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border Name="Border" Background="{TemplateBinding Background}"
                                    BorderThickness="1" BorderBrush="#CCC" CornerRadius="4,4,0,0">
                                <ContentPresenter x:Name="ContentSite"
                                                  VerticalAlignment="Center"
                                                  HorizontalAlignment="Center"
                                                  ContentSource="Header"
                                                  Margin="12,2,12,2"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="#FFF" />
                            </Trigger>
                            <Trigger Property="IsSelected" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="#EEE" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>

    <TabItem>
        <TabItem.Header>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="Tab 1" />
                <TextBlock Text="Subtext" FontSize="12" />
            </StackPanel>
        </TabItem.Header>
        <TabItem.Content>
            <TextBlock Text="Tab 1 Content" />
        </TabItem.Content>
    </TabItem>

    <TabItem>
        <TabItem.Header>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="Tab 2" />
                <TextBlock Text="Subtext" FontSize="12" />
            </StackPanel>
        </TabItem.Header>
        <TabItem.Content>
            <TextBlock Text="Tab 2 Content" />
        </TabItem.Content>
    </TabItem>

</TabControl>

In this example, we define a TabControl and add two TabItems with Headers containing a StackPanel with two TextBlocks - one for the main text and one for the subtext. The TabItem Content is set using a TextBlock.

We also define a custom ControlTemplate for the TabItem in the TabControl.Resources, which sets the look and feel for the tabs.

The result will be tabs with text displayed under them, like so:

xaml tabs with text under them

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-01 11:00:00 +0000

Seen: 9 times

Last updated: Apr 09 '22