【问题标题】:How to collapse all Group Expander in ListView?如何折叠 ListView 中的所有 Group Expander?
【发布时间】:2019-09-13 15:14:12
【问题描述】:

我有一个ListView,上面有一个GroupStyle。在我的风格中,我有一个Expander。我想在ListView 中使用ContextMenu 一键折叠和展开所有组,我想通过单击扩展器来展开每个组。如何获取组然后以编程方式扩展它?

<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 做折叠和展开所有组的示例代码?我没有找到任何东西。

【问题讨论】:

  • 将 IsExpanded 绑定到 itemssource 中的新属性,然后将所有项目的该属性设置为 false。

标签: wpf


【解决方案1】:

您可以将IsExpanded 属性绑定到ListViewTag 属性:

<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,并通过在可视化树中遍历它们来设置每个GroupItemTag 属性:

private void menuItemPropertyExpanderCollapse_Click(object sender, RoutedEventArgs e)
{
    foreach (GroupItem gi in FindVisualChildren<GroupItem>(PropertyChangeList))
        gi.Tag = false;
}

private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

XAML:

<Expander Header="{Binding Name}"
          IsExpanded="{Binding Tag, RelativeSource={RelativeSource AncestorType=GroupItem}, TargetNullValue=true, FallbackValue=true}">
    <ItemsPresenter />
</Expander>

【讨论】:

  • 你答对了,但我忘了写更多细节。是的,现在我可以展开和折叠所有组,但我不能再展开单个组。这是一个全有或全无的事情。我的问题遗漏了一些重要的细节 :-( 我更新了我的问题文本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 2011-05-25
  • 2011-02-19
  • 2021-07-09
  • 1970-01-01
  • 2012-01-29
相关资源
最近更新 更多