动态隐藏 WPF TabItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35513194/
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-13 13:41:15  来源:igfitidea点击:

Dynamically hide a WPF TabItem

wpftabcontroltabitem

提问by hardywang

Let's say I have a very simple XAML

假设我有一个非常简单的 XAML

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TabControl>
            <TabItem Header="Tab 1" Visibility="Hidden">
                <TextBlock>shfsdjkfhksh jkfhd sfjdklh sfjdkh fjdkh fdjhf </TextBlock>
            </TabItem>
            <TabItem Header="Tab 2" Visibility="Hidden">
                <TextBlock>3807689vthvybhgthugbbjgkngoebt4uibn54</TextBlock>
            </TabItem>
        </TabControl>
    </StackPanel>
</Window>

If I just set TabItem's visibility to hidden, the content inside that tab does not hide.

如果我只是将TabItem的可见性设置为隐藏,则该选项卡内的内容不会隐藏。

Is there a way to hide tab header and its content together?

有没有办法将标签页头及其内容隐藏在一起?

采纳答案by HoboCannibaL

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.

您可以通过将 Visibility 绑定到父控件来做到这一点。如果您使用的是视图模型,则可以将可见性绑定到视图模型中的属性,并将该属性用于 TabItem 和 TextBlock。

<StackPanel>
    <TabControl>
        <TabItem x:Name="tab1" Header="Tab 1" Visibility="Hidden">
            <TextBlock Visibility="{Binding Path=Visibility, ElementName=tab1}">shfsdjkfhksh jkfhd sfjdklh sfjdkh fjdkh fdjhf</TextBlock>
        </TabItem>
    </TabControl>
</StackPanel>

回答by Guttsy

If you set Visibilityto Hiddenfor the selected/active tab, you'll need to select a different tab, e.g.

如果您为选定/活动选项卡设置VisibilityHidden,则需要选择不同的选项卡,例如

<StackPanel>
    <TabControl>
        <TabItem x:Name="T1" Header="Tab 1" Visibility="Hidden" >
            <TextBlock>1111111111111111111</TextBlock>
        </TabItem>
        <TabItem x:Name="T2" Header="Tab 2" IsSelected="True">
            <TextBlock>22222222222222222222222</TextBlock>
        </TabItem>
    </TabControl>
</StackPanel>

You do not need to hide the content of the TabItemprovided that the TabItemis hidden and unselected.

你并不需要隐藏的内容TabItem所提供的TabItem是隐藏的,不选。