2016-09-17 66 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