Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Tab headers in WPF TabControl

Tags:

wpf

tabcontrol

What is the best way to hide Tab headers when there is only a single visible Tab?

I want to hide TabControl chrome completely, while leaving the content of the Tab visible.

like image 691
Andrey Shchekin Avatar asked Dec 22 '08 21:12

Andrey Shchekin


People also ask

How do I hide a tab in WPF?

You can do that by binding the Visibility to the parent control. If you are using a view model, you can bind the visibility to a property in your view model and use the property for both the TabItem and TextBlock.

How do I hide tabs in TabControl?

How to Hide Tabs. To hide tabs in IntegralUI TabControl is simple, just set the Visible property value to false, and the designated tab will become hidden.


2 Answers

You can use a Style applied to TabItem with a DataTrigger that will collapse it if the parent TabControl has only one item:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <x:Array x:Key="tabData" Type="{x:Type sys:String}">
            <sys:String>do</sys:String>
            <sys:String>re</sys:String>
            <sys:String>mi</sys:String>
        </x:Array>
    </Grid.Resources>
    <TabControl ItemsSource="{StaticResource tabData}">
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}, Path=Items.Count}" Value="1">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>                
            </Style>
        </TabControl.ItemContainerStyle>
    </TabControl>
</Grid>

If you want to get rid of the TabControl completely if there is only one item, that logic should probably be at a higher level.

like image 177
Robert Macnee Avatar answered Oct 17 '22 02:10

Robert Macnee


And if you have to do it in code behind....

foreach (var item in tabControl.Items)
            (item as TabItem).Visibility = tabControl.Items.Count > 1 ? Visibility.Visible : Visibility.Collapsed;
like image 1
oo_dev Avatar answered Oct 17 '22 02:10

oo_dev