當你壓扁這個層次的分組會的工作即成爲「讓每個單組重複的聯繫人」「有組聯繫」 ......
例如
如果你有4項與組像...
Dog { mammal, quadruped }
Man { mammal, biped }
PrayingMantis { insect, quadruped }
Pegion { bird, biped }
然後你新平面列表應該是這樣的......
<mammal, Dog>
<mammal, Man>
<bird, Pigeon>
<insect, PrayingMantis>
<biped, Man>
<biped, Pigeon>
<quadruped, Dog>
<quadruped, PrayingMantis>
所以applyin 分組後上Keys
它應該是
mammal { Dog, Man }
bird { Pigeon }
insect { PrayingMantis }
biped { Man, Pigeon }
quadruped { Dog, PrayingMantis }
C#代碼:
//Flatten the groups into a KeyValuePair<string, Contacts> list using LINQ.
var flatGroups
= listGroups.SelectMany(
ctc => ctc.Groups.Select(
grp => new KeyValuePair<string, Contact>(grp, ctc))).ToList();
//Apply CollectionViewSource group on the `Key`.
var collectionVwSrc = new CollectionViewSource();
collectionVwSrc.Source = flatGroups;
collectionVwSrc.GroupDescriptions.Add(new PropertyGroupDescription("Key"));
//Apply groups as itemssource to the TreeView.
MyGroupsTree.ItemsSource = collectionVwSrc.View.Groups;
XAML
<TreeView x:Name="MyGroupsTree">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<!--GroupItem.Name-->
<TextBlock Text="{Binding Path=Name}"
FontWeight="Bold"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<!--Contact.Name-->
<TextBlock Text="{Binding Value.Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
讓我知道如果這能幫助...
感謝您的答覆。它工作正常! – tesla1060 2012-08-05 11:34:09
@ user1569058,如果這篇文章對你有所幫助,那麼請把它標記爲aswer。 – 2012-08-06 05:35:08
乾淨的答案,不知道爲什麼沒有給出+1。無論如何,從我的+1 :) – Doug 2014-10-14 18:47:54