我试图在我的 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) Matches
为 Nation
和 联赛
.
为此,我声明了一个 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;
}
本质上,我提取了列表的组并试图在嵌套组中找到 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/