2016-09-17 67 views
8

我试图找到我的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对于NationLeague,绑定为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 

这是组织,现在想象为EnglandPremier League并为AfghanistanAfghan 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; 
} 

基本上我提取列表的集团,并试图找到切换按钮上的巢群,但我不能找到它。有人可以帮助我吗?

回答

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 
    } 
} 
+0

感谢您的回答,我想你的代码,但我有一个问题,变量'toggleButton'是增值的用'null'值。这是您调用FindName方法的属性'Template'的内容的图像:http://imgur.com/a/EqY6k – Unchained

+0

您可以确认:** a)**您已分配了一个名称与'x:Name =“PART_ToggleButton”'给模板内部的控件,** b)**您正在调用'FindName',其名称与_a中提到的名称完全相同_(“PART_ToggleButton”仅仅是一个例子,你可以改变它,但它必须在两个地方都是一样的),并且** c)**名称被分配到的控件**实际上是一个'ToggleButton'(或者从它派生出来)? – Grx70

+0

我终于找到了问题。所以我尝试了你的代码把'ToggleButton'放在'Template'中,并且运行良好,第一次尝试它不起作用,因为我用我自己的风格覆盖了ToggleButton。所以在'Template'中我有一个'UserControl'并且里面有'ToggleButton',更多细节我创建了一个粘贴:http://pastebin.com/CXybKUFn我还添加了一些注释来解释更好的情况。 – Unchained