我有带有分组项目的 ListView。分组使用自定义 GroupStyle (Expander)。我想要一个复选框,它会在何时展开和折叠所有组。在我手动单击组标题并展开或折叠该组之前,它工作正常。单击该特定组后停止响应复选框选择。用户手动单击组后,绑定似乎被破坏。
请告知我做错了什么。
非常感谢。
真诚的,弗拉德。
<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Window.Resources>
<XmlDataProvider x:Key="MyData" XPath="/Info">
<x:XData>
<Info xmlns="">
<Item Name="Item 1" Category="Cat1" />
<Item Name="Item 2" Category="Cat1" />
<Item Name="Item 3" Category="Cat2" />
<Item Name="Item 4" Category="Cat2" />
<Item Name="Item 5" Category="Cat2" />
<Item Name="Item 6" Category="Cat3" />
</Info>
</x:XData>
</XmlDataProvider>
<CollectionViewSource x:Key='src' Source="{Binding Source={StaticResource MyData}, XPath=Item}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@Category" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<ControlTemplate x:Key="ListTemplate" TargetType="ListView">
<ListView BorderThickness="0"
ItemsSource='{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource}'
DisplayMemberPath="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DisplayMemberPath}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="{Binding IsChecked, ElementName=chkExpandAll, Mode=OneWay}">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100" />
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<CheckBox Name="chkExpandAll" IsChecked="True" Content="Expand All" />
<ListView ItemsSource='{Binding Source={StaticResource src}}' DisplayMemberPath="@Name" BorderThickness="1" Template="{StaticResource ListTemplate}" />
</StackPanel>
</Window>