Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically hide a WPF TabItem

Let's say I have a very simple 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.

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

like image 824
hardywang Avatar asked May 24 '26 18:05

hardywang


1 Answers

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.

<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>
like image 70
HoboCannibaL Avatar answered May 26 '26 21:05

HoboCannibaL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!