简体   繁体   中英

Use a variable to set visibility of TabItem

In WPF how does one show/hide a TabItem depending on the value of an Int variable by using a style?

Style Attempted

<Style x:Key="TabItemStyleVisibility"
       TargetType="{x:Type TabItem}"
       BasedOn="{StaticResource {x:Type TabItem}}">            
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=TabItemIndex, 
                                           Mode=OneWay,
                                           UpdateSourceTrigger=PropertyChanged}" 
                         Value="1">
                <Setter Property="Visibility" Value="Visible"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

TabItem Xaml :

<TabItem Name="Tab1" Style="{StaticResource TabItemStyleVisibility}">

Alternate Attempt

Possibly a Style for each tab (copy/paste) and change the value each time:

<DataTrigger Binding="{Binding Path=TabItemIndex, 
                               Mode=OneWay, 
                               UpdateSourceTrigger=PropertyChanged}"                
             Value="1">
</DataTrigger>

But it's will not be clean (one style per TabItem).

Is there is a way to define the tabItem visibility depending on the name of the tabItem or depending of an int and have the same style for every TabItem?

You can use a converter to do that, here's an example on how to do that (untested)

class MyConverter : IValueConverter //Implement this one
{
    public object Convert(object state....)
    {
        string name = (string)state;
        if(name == "something") 
          return Visiblity.Visible;
        else return Visibilty.Hidden;
    }
}

<Grid.Resources>
    <myConverterNamespace:MyConverter x:Key = "myConverter"/>
</Grid.Resource>

    <TabItem Name="Tab1" Style="{StaticResource TabItemStyleVisibility}" 
Visbility={Binding ElementName="someTabItemName" Path="Name" Converter={StaticResource myConverter}}>

我弄错了我要尝试的可视性,我要做的就是在SelectedIndex上工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM