c# - 无法在 CollectionViewGroup 中找到 ToggleButton

标签 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 过滤 ObservableCollection 绑定(bind) MatchesNation联赛.

为此,我声明了一个 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 

这是组织,现在想象一下 EnglandPremier LeagueAfghanistanAfghan 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;
}

本质上,我提取了列表的组并试图在嵌套组中找到 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
    }
}

关于c# - 无法在 CollectionViewGroup 中找到 ToggleButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39547304/

相关文章:

wpf - ItemsSource 更改时如何刷新 TreeView ?

c# - DotNet 中 Textbox/RichTextbox 内部的组件

c# - 将项目绑定(bind)到 ListBox 多列

c# - 抑制系统覆盖,Windows Phone 8.1 (Silverlight)

c# - 何时使用 Cast 或 Convert

c# - 使用流编写器集成测试功能

c# - 是否可以将第一个字母变大并使其余文本围绕它对齐

c# - 如何安装扩展的 WPF 工具包?

c# - 每行设置 XAML Datagrid 文本颜色

Wpf Material Design 日期时间选择器