【发布时间】: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