无法在CollectionViewGroup中找到ToggleButton

时间:2016-09-17 13:20:36

标签: c# wpf xaml

我试图在我的CollectionViewGroup中找到关联的ToggleButton,我的xaml结构如下:

 <UserControl.Resources>
    <CollectionViewSource Source="{Binding Matches}" x:Key="GroupedItems">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="MatchNation" />
            <PropertyGroupDescription PropertyName="MatchLeague" />
        </CollectionViewSource.GroupDescriptions>
</UserControl.Resources>

如何看待我CollectionViewGroup过滤了ObservableCollectionMatches的{​​{1}}绑定Nation

为此,我宣布League有两个ListView,一个过滤GroupStyle,另一个过Country,在这段代码中我只添加第二个GroupStyle(包含ToggleButton):

League

那么,你如何看待第二组样式(<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> )我是一个ToggleButton。

现在,GroupStyle将根据Nation -> League中可用的项目重复,例如:

ObservableCollection

这是该组织,现在想象|England |Premier League 1. item 2. item 3. item |Afghanistan |Afghan Premier League 1. item 2. item Premier LeagueEngland Afghan Premier League Afghanistan我有ToggleButton在右侧插入,我需要获取列表中每个ToggleButtons的所有Group。我试过这个:

var playingListSource = (ListCollectionView).Playing.Items.SourceCollection;

foreach (var gp in playingListSource.Groups)
{
       var rootGroup = (CollectionViewGroup)gp; //Convert the nested group
       var parentGroup = rootGroup.Items;
}

基本上我提取列表的组并尝试在nester组上找到ToggleButton,但我找不到它。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:3)

首先,如果我们将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
    }
}