我試圖找到我的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