我试图找到我的CollectionViewGroup相关的ToggleButton
,我的XAML结构如下:无法找到切换按钮在CollectionViewGroup
<UserControl.Resources>
<CollectionViewSource Source="{Binding Matches}" x:Key="GroupedItems">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="MatchNation" />
<PropertyGroupDescription PropertyName="MatchLeague" />
</CollectionViewSource.GroupDescriptions>
</UserControl.Resources>
如何你可以看到我有一个CollectionViewGroup
该过滤器的ObservableCollection
对于Nation
和League
,绑定为Matches
。
对于此,我宣布一个ListView
具有两个GroupStyle
,一个过滤器Country
,而另一个用于League
,在这一块的代码,我只加了第二GroupStyle(包含切换按钮):
<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'是增值的用'null'值。这是您调用FindName方法的属性'Template'的内容的图像:http://imgur.com/a/EqY6k – Unchained
您可以确认:** a)**您已分配了一个名称与'x:Name =“PART_ToggleButton”'给模板内部的控件,** b)**您正在调用'FindName',其名称与_a中提到的名称完全相同_(“PART_ToggleButton”仅仅是一个例子,你可以改变它,但它必须在两个地方都是一样的),并且** c)**名称被分配到的控件**实际上是一个'ToggleButton'(或者从它派生出来)? – Grx70
我终于找到了问题。所以我尝试了你的代码把'ToggleButton'放在'Template'中,并且运行良好,第一次尝试它不起作用,因为我用我自己的风格覆盖了ToggleButton。所以在'Template'中我有一个'UserControl'并且里面有'ToggleButton',更多细节我创建了一个粘贴:http://pastebin.com/CXybKUFn我还添加了一些注释来解释更好的情况。 – Unchained