如何隐藏wpf扩展器头

时间:2014-08-19 20:11:49

标签: c# wpf listview expander

我在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);

1 个答案:

答案 0 :(得分:0)

也许有人还需要它...

<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>