无法在CollectionViewGroup中找到ToggleButton
本文关键字:ToggleButton CollectionViewGroup | 更新日期: 2023-09-27 18:07:17
我试图找到一个ToggleButton
关联在我的CollectionViewGroup,我的xaml结构如下:
<UserControl.Resources>
<CollectionViewSource Source="{Binding Matches}" x:Key="GroupedItems">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="MatchNation" />
<PropertyGroupDescription PropertyName="MatchLeague" />
</CollectionViewSource.GroupDescriptions>
</UserControl.Resources>
你怎么能看到我有一个CollectionViewGroup
过滤ObservableCollection
结合的Matches
为Nation
和League
。
为此,我声明了一个ListView
,其中有两个GroupStyle
,一个用于Country
和另一个用于League
,在这段代码中,我只添加了第二个GroupStyle(包含ToggleButton):
<ListView ItemsSource="{Binding Source={StaticResource GroupedItems}}">
<!-- this is the second group style -->
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True" Background="#4F4F4F">
<Expander.Header>
<DockPanel Height="16.5">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="White" FontSize="11.5" VerticalAlignment="Bottom" />
<ToggleButton Checked="{Binding IsFavourite}" HorizontalAlignment="Right"/>
</DockPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
那么,你可以看到在第二组样式(Nation -> League
)我有一个ToggleButton。
现在,GroupStyle将根据ObservableCollection
中可用的项进行重复,例如:
|England
|Premier League
1. item
2. item
3. item
|Afghanistan
|Afghan Premier League
1. item
2. item
这就是组织,现在想象一下,对于England
的Premier League
和Afghanistan
的Afghan Premier League
,我在右边插入了一个ToggleButton
,我需要在列表中获得每个Group
的所有ToggleButtons
。我试过了:
var playingListSource = (ListCollectionView).Playing.Items.SourceCollection;
foreach (var gp in playingListSource.Groups)
{
var rootGroup = (CollectionViewGroup)gp; //Convert the nested group
var parentGroup = rootGroup.Items;
}
基本上,我提取了列表的Group,并试图找到巢组上的ToggleButton,但我找不到它。有人能帮帮我吗?
首先,如果我们命名ToggleButton
以便稍后可以使用ControlTemplate.FindName
方法,事情将会容易得多。这里是ToggleButton
:
<ToggleButton x:Name="PART_ToggleButton"
Checked="{Binding IsFavourite}"
HorizontalAlignment="Right" />
我们现在需要的是得到模板化的容器(一个GroupItem
控件)。我们可以使用ListView.ItemContainerGenerator.ContainerFromItem
方法。
知道这里有一段代码应该检索有问题的ToggleButton
:
//we assume listView is a reference to the ListView
var playingListSource = (ListCollectionView)listView.ItemsSource;
//first we iterate over the top-level groups
foreach (CollectionViewGroup nationGroup in playingListSource.Groups)
{
//second-level groups are items of their parents
foreach (CollectionViewGroup leagueGroup in nationGroup.Items)
{
//first we retrieve the GroupItem control associated with current second-level group
var container = listView.ItemContainerGenerator.ContainerFromItem(leagueGroup) as GroupItem;
//then we find the named ToggleButton inside the template
var toggleButton = container.Template.FindName("PART_ToggleButton", container) as ToggleButton;
//at this point we have a reference to the ToggleButton
}
}