简体   繁体   中英

Hide header if no items inside the expander

I would know if there is some fast way to hide all expander header if there are no items inside the expander content.

My expander:

<Expander IsExpanded="True" Loaded="Expander_Loaded" Visibility="{Binding Items[0],Converter={StaticResource collectionVisibilityHeaderConverter}}">
    <Expander.Header>
        <DockPanel HorizontalAlignment="Stretch" >
            <TextBlock Text="{Binding Path=Name}" FontSize="18"></TextBlock>
            <Button Style="{StaticResource ButtonStyle}" x:Name="ShowAllButton" Content=" SHOW ALL " HorizontalAlignment="Right" DockPanel.Dock="Right"  Padding="15" Margin="0,0,15,0" Click="ShowAllButton_Click"></Button>
            <Button Style="{StaticResource ButtonStyle}" x:Name="ShowOnlyButton" Content=" SHOW ONLY " HorizontalAlignment="Right" DockPanel.Dock="Right" Padding="15" Margin="0,0,15,0" Click="ShowOnlyButton_Click"></Button>
        </DockPanel>
    </Expander.Header>
    <Expander.Style>
        <Style TargetType="{x:Type Expander}">
            <Setter Property="Background" Value="#ccf2ff"></Setter>
            <Setter Property="TextElement.FontFamily" Value="Arial Nova"/>
        </Style>
    </Expander.Style>
    <Expander.Content>
        <ItemsPresenter />
    </Expander.Content>
</Expander>

I'm using now an converter, and basically it works, but I think there are easier way.

You could use a DataTrigger in the Expander Style :

<Expander IsExpanded="True" Loaded="Expander_Loaded">
    <Expander.Header>
        <DockPanel HorizontalAlignment="Stretch" >
            <TextBlock Text="{Binding Path=Name}" FontSize="18"></TextBlock>
            <Button Style="{StaticResource ButtonStyle}" x:Name="ShowAllButton" Content=" SHOW ALL " HorizontalAlignment="Right" DockPanel.Dock="Right"  Padding="15" Margin="0,0,15,0" Click="ShowAllButton_Click"></Button>
            <Button Style="{StaticResource ButtonStyle}" x:Name="ShowOnlyButton" Content=" SHOW ONLY " HorizontalAlignment="Right" DockPanel.Dock="Right" Padding="15" Margin="0,0,15,0" Click="ShowOnlyButton_Click"></Button>
        </DockPanel>
    </Expander.Header>
    <Expander.Style>
        <Style TargetType="{x:Type Expander}">
            <Setter Property="Background" Value="#ccf2ff"></Setter>
            <Setter Property="TextElement.FontFamily" Value="Arial Nova"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Items.Count}" Value="0">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Expander.Style>
    <Expander.Content>
        <ItemsPresenter />
    </Expander.Content>
</Expander>

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