WPF TabItem設置Visibility隱藏Control內容

源自MSDN問題。spa

思路很簡答:3d

TabControl由於只顯示TabItem的選擇項的control。code

因此單獨的設置tabitem的control或者使用control的觸發器都是不起做用的。blog

只有用controltemplate。get

關鍵代碼以下:it

<Style x:Key="c1" TargetType="TabItem">
            <Style.Triggers>
                <!--關鍵代碼 能夠多寫一個hidden-->
                    <DataTrigger Binding="{Binding Visibility,RelativeSource={RelativeSource Mode=Self} }" Value="Collapsed">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ContentControl Visibility="Collapsed" Content="{Binding}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <!--關鍵代碼-->
                <DataTrigger Binding="{Binding Visibility ,RelativeSource={RelativeSource Mode=Self} }" Value="Visible">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ContentControl Visibility="Visible" Content="{Binding}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

經過數據觸發器來檢測tabitem的visibility的屬性來設置control屬性。io

完整代碼class

<Window.Resources>
        <Style x:Key="c1" TargetType="TabItem">
            <Style.Triggers>
                <!--關鍵代碼 能夠多寫一個hidden-->
                    <DataTrigger Binding="{Binding Visibility,RelativeSource={RelativeSource Mode=Self} }" Value="Collapsed">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ContentControl Visibility="Collapsed" Content="{Binding}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <!--關鍵代碼-->
                <DataTrigger Binding="{Binding Visibility ,RelativeSource={RelativeSource Mode=Self} }" Value="Visible">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ContentControl Visibility="Visible" Content="{Binding}"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="t1" BasedOn="{StaticResource c1}" TargetType="TabItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=cb1,Path=IsChecked}" Value="true">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="t2" BasedOn="{StaticResource c1}" TargetType="TabItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=cb2,Path=IsChecked}" Value="true">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <StackPanel>
            <CheckBox x:Name="cb1">
                <CheckBox.Content>
                    <TextBlock>
                        <Run Text="Item1 狀態:"/>
                        <Run Text="{Binding ElementName=item1,Path=Visibility,Mode=OneWay}"/>
                    </TextBlock>
                </CheckBox.Content>
            </CheckBox>
            <CheckBox  x:Name="cb2">
                <CheckBox.Content>
                    <TextBlock>
                        <Run Text="Item2 狀態:"/>
                        <Run Text="{Binding ElementName=item2,Path=Visibility,Mode=OneWay}"/>
                    </TextBlock>
                </CheckBox.Content>
            </CheckBox>
        </StackPanel>
        <TabControl Grid.Column="1"  >
            <TabItem Header="item1" x:Name="item1"  Style="{StaticResource t1}" >
                 
                <Grid Background="Red">
                    
                </Grid>
            </TabItem>
            <TabItem Header="item2"  x:Name="item2" Style="{StaticResource t2}">
                <Grid  Background="Black">

                </Grid>
            </TabItem>
        </TabControl>
    </Grid>

截圖sed

相關文章
相關標籤/搜索