如何隐藏wpf扩展头

本文关键字:wpf 扩展 隐藏 何隐藏 | 更新日期: 2023-09-27 18:18:39

我在listview中有一个展开器。它附加了几个分组,问题是有时分组名称不适用于某些行。因此,它显示了一个空的扩展头,在这种情况下,我希望隐藏。任何帮助都将不胜感激。谢谢。

交货。并不是所有的东西都有一个"组"。

<ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander IsExpanded="True" >
                                        <Expander.HeaderTemplate>
                                            <DataTemplate>
                                            </DataTemplate>
                                        </Expander.HeaderTemplate>
                                            <Expander.Header>
                                            <StackPanel Orientation="Horizontal" >
                                                <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="14" VerticalAlignment="Bottom" />
                                                <TextBlock Text="{Binding ItemCount}" FontSize="14" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>

items.Add(new User() { Name = "D", Age = 42, Question = "First", Category = "Dependecies",Character="Blank" });
        items.Add(new User() { Name = "E", Age = 39, Question = "First", Category = "Variables", Expression = "Exp1" });
        items.Add(new User() { Name = "F", Age = 13, Question = "First", Category = "Rules", Expression = "Exp1" });
        items.Add(new User() { Name = "V", Age = 13, Question = "First", Category = "Rules", Expression = "Exp1", Group = "A" });
        items.Add(new User() { Name = "W", Age = 13, Question = "First", Category = "Rules", Expression = "Exp1", Group = "A" });
        items.Add(new User() { Name = "Z", Age = 13, Question = "First", Category = "Rules", Expression = "Exp1", Group = "B" });
            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(listView.ItemsSource);
        PropertyGroupDescription groupDescription = new PropertyGroupDescription("Question");
        view.GroupDescriptions.Add(groupDescription);
        CollectionView view2 = (CollectionView)CollectionViewSource.GetDefaultView(listView.ItemsSource);
        PropertyGroupDescription groupDescription2 = new PropertyGroupDescription("Category");
        view2.GroupDescriptions.Add(groupDescription2);
        CollectionView view3 = (CollectionView)CollectionViewSource.GetDefaultView(view2);
        PropertyGroupDescription groupDescription3 = new PropertyGroupDescription("Group");
        view3.GroupDescriptions.Add(groupDescription3);

如何隐藏wpf扩展头

也许有人还需要它…

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <!-- STEP 1: ADD MOTHER CONTAINER -->
                            <Grid>
                                <Expander x:Name="expander1" IsExpanded="True" >
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal" >
                                            <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="14" VerticalAlignment="Bottom" />
                                            <TextBlock Text="{Binding ItemCount}" FontSize="14" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                                <!-- STEP 2: THIS IS THE TRICK  Visibility="Collapsed"  -->
                                <ItemsPresenter x:Name="item" Visibility="Collapsed" />
                            </Grid>
                            <!-- STEP 3: THIS IS THE MAGIC, ControlTemplate.Triggers   -->
                            <ControlTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=Name}" Value="{x:Null}">
                                    <Setter TargetName="expander1" Property="Visibility" Value="Collapsed"></Setter>
                                    <Setter TargetName="item" Property="Visibility" Value="Visible"></Setter>
                                </DataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListView.GroupStyle>