如果扩展器中没有项目,请隐藏标题

时间:2017-04-03 12:42:54

标签: c# wpf header 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>

我现在使用转换器,基本上它可以工作,但我认为有更简单的方法。

1 个答案:

答案 0 :(得分:0)

您可以在DataTrigger 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>