如何在ListView中折叠所有Group Expander?

时间:2019-04-24 10:38:21

标签: wpf

我有一个ListView,上面有GroupStyle。在样式上,我有一个Expander。我想在ContextMenu中使用ListView来折叠和扩展所有组,而我想通过单击扩展器来扩展每个组。我如何获得组,然后以编程方式进行扩展?

<Style x:Key="PropertyGroupStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Expander Header="{Binding Name}" IsExpanded="True">                               
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>                    
            </Setter>                
        </Style>



<ListView Name="PropertyChangeList"
                                IsSynchronizedWithCurrentItem="True" Height="Auto"                     

                                ItemsSource="{Binding}"                 
                             >
                                <ListView.GroupStyle>
                                    <GroupStyle ContainerStyle="{StaticResource PropertyGroupStyle}"/>
                                </ListView.GroupStyle>
                                <ListView.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Name="menuItemPropertyExpanderCollapse"
                                            Header="{Binding Path=labelCollapse, FallbackValue='Collapse'}"
                                            Click="menuItemPropertyExpanderCollapse_Click" 
                                                  />
                                        <MenuItem Name="menuItemPropertyExpanderExpand"
                                            Header="{Binding Path=labelExpand, FallbackValue='Expand'}"

                                  />
                                    </ContextMenu>
                                </ListView.ContextMenu>
                                <ListView.View>
                                    <GridView AllowsColumnReorder="False" >
                                        <GridViewColumn Header="Date Occured"
                                                Width="20"
                                                DisplayMemberBinding="{Binding DateOccured}" />

                                        <GridViewColumn Header="PropertyName"
                                                Width="Auto"
                                                DisplayMemberBinding="{Binding PropertyName}"/>                                          

                                    </GridView>
                                </ListView.View>
                            </ListView>

ICollectionView PropertyListview = CollectionViewSource.GetDefaultView(hPropList);
        PropertyListview.GroupDescriptions.Add(new PropertyGroupDescription("PropertyName"));
        PropertyListview.SortDescriptions.Add(new SortDescription("PropertyName", ListSortDirection.Ascending));
        PropertyListview.SortDescriptions.Add(new SortDescription("DateOccurred", ListSortDirection.Ascending));

        PropertyChangeList.ItemsSource = PropertyListview;

有人用示例代码进行折叠并使用ContextMenu展开所有组吗?我什么都没找到。

1 个答案:

答案 0 :(得分:1)

您可以将IsExpanded属性绑定到Tag的{​​{1}}属性:

ListView

...并在事件处理程序中设置<Style x:Key="PropertyGroupStyle" TargetType="{x:Type GroupItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Expander Header="{Binding Name}" IsExpanded="{Binding Tag, RelativeSource={RelativeSource AncestorType=ListView}, TargetNullValue=true, FallbackValue=true}"> <ItemsPresenter /> </Expander> </ControlTemplate> </Setter.Value> </Setter> </Style> 属性:

Tag
  

您正确回答了问题,但是我忘了写更多细节。是的,现在我可以扩展和折叠所有组,但现在不能再扩展单个组。这是全有还是全无的事情。我的问题错过了一些重要的细节:-(我更新了问题文本。

将绑定的private void menuItemPropertyExpanderCollapse_Click(object sender, RoutedEventArgs e) { PropertyChangeList.Tag = false; } 更改为AncestorType,并通过在可视树中对其进行遍历来设置每个GroupItem的{​​{1}}属性:

Tag

XAML:

GroupItem