在 WPF TabControl 中隐藏 Tab 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/387480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:54:23  来源:igfitidea点击:

Hide Tab headers in WPF TabControl

wpftabcontrol

提问by Andrey Shchekin

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

当只有一个可见的 Tab 时,隐藏 Tab 标题的最佳方法是什么?

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

我想完全隐藏 TabControl 镶边,同时让 Tab 的内容可见。

回答by Robert Macnee

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

您可以将应用于 TabItem 的 Style 与 DataTrigger 一起使用,如果父 TabControl 只有一项,它将折叠它:

<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.

如果您想在只有一项的情况下完全摆脱 TabControl,那么该逻辑可能应该处于更高的级别。

回答by oo_dev

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;