Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to find ToggleButton in CollectionViewGroup

Tags:

c#

wpf

xaml

I'm trying to find a ToggleButton associated in my CollectionViewGroup, my xaml structure is the following:

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

How you can see I've a CollectionViewGroup that filter the ObservableCollection binded Matches for Nation and League.

For this I've declared a ListView that have two GroupStyle, one that filter for Country and another for League, in this piece of code I add only the second GroupStyle (that contains the 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>

So, how you can see in the second group style (Nation -> League) I've a ToggleButton.

Now the GroupStyle will be repeated based on the items available in the ObservableCollection, so for example:

|England
    |Premier League
     1. item  
     2. item 
     3. item 
|Afghanistan
     |Afghan Premier League
     1. item 
     2. item 

this is the organization, now imagine that for Premier League of England and for Afghan Premier League of Afghanistan there is a ToggleButton that I've inserted on the right, I need to get all the ToggleButtons of each Group available on the list. I tried this:

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

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

Essentially I extract the Group of the list and tried to find the ToggleButton on the nester group, but I cannot find it. Someone could help me?

like image 829
Unchained Avatar asked Sep 17 '16 13:09

Unchained


1 Answers

First of all, things will be a lot easier if we name the ToggleButton so that we can later use the ControlTemplate.FindName method. So here's the ToggleButton:

<ToggleButton x:Name="PART_ToggleButton"
              Checked="{Binding IsFavourite}"
              HorizontalAlignment="Right" />

What we need now is to get our hands on the templated container (a GroupItem control). For that we can use the ListView.ItemContainerGenerator.ContainerFromItem method.

Knowing that here's a piece of code that should retrieve the ToggleButton in question:

//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
    }
}
like image 169
Grx70 Avatar answered Nov 15 '22 07:11

Grx70