我正在尝试模仿msn Messenger通讯录的树状视图显示。我有一个Contact对象,该对象具有Groups属性,该属性返回该Contact所属的数组msngroups,这表示我按照下面的内容
Class Contact
{
string Name;
string[] Groups {get;set;}
}
ObservableCollection<Contact> ContactList;
因此联系人可以在多个组中,是否可以使用CollectionViewSource生成正确的视图来提供wpf TreeView?
[当您扁平化此层次结构时,即“具有组的联系人”变为“具有每个组的重复联系人”时,分组将起作用...
例如
如果您有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>
因此,在上方的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>
让我知道这是否有帮助...
////DataItem.cs
public class DataItem
{
public string Name { get; set; }
public string Path { get; set; }
public string[] GroupProperties { get; set; }
}
////MainWindow.xaml.cs
public partial class MainWindow : Window
{
public ObservableCollection<DataItem> DataList { get; set; }
public MainWindow()
{
DataList = new ObservableCollection<DataItem>(new DataItem[] {
new DataItem(){ Name = "1111", Path = "C:\\1111", GroupProperties = new string[]{"HeNan", "JiangSu", "BeiJing"} },
new DataItem(){ Name = "2222", Path = "C:\\2222", GroupProperties = new string[]{"HeNan", "TianJin", "ShenZhen"} },
new DataItem(){ Name = "3333", Path = "C:\\1111", GroupProperties = new string[]{"GuangZhou", "XiAn", "BeiJing"} },
new DataItem(){ Name = "4444", Path = "C:\\4444", GroupProperties = new string[]{"HeNan", "NanJing", "KunMing"} }
});
InitializeComponent();
}
}
////MainWindow.xaml
<Window x:Class="CultureDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<CollectionViewSource x:Key="ListBoxSource2" Source="{Binding DataList}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription Direction="Descending" PropertyName="Name">
</scm:SortDescription>
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Path" StringComparison="OrdinalIgnoreCase"></PropertyGroupDescription>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<CollectionViewSource x:Key="ListBoxSource3" Source="{Binding DataList}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription Direction="Ascending" PropertyName="Name">
</scm:SortDescription>
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="GroupProperties" StringComparison="OrdinalIgnoreCase"></PropertyGroupDescription>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180*" />
<ColumnDefinition Width="169*" />
<ColumnDefinition Width="154*" />
</Grid.ColumnDefinitions>
<ListBox Name="listBox1" ItemsSource="{Binding DataList}" DisplayMemberPath="Name">
</ListBox>
<ListBox Grid.Column="1" Name="listBox2" ItemsSource="{Binding Source={StaticResource ListBoxSource2}}" >
<ListBox.GroupStyle>
<GroupStyle>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Grid.Column="2" Name="listBox3" ItemsSource="{Binding Source={StaticResource ListBoxSource3}}">
<ListBox.GroupStyle>
<GroupStyle>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Bea Stollnitzcovered this exact situation.CollectionViewSource
支持定义GroupDescriptions
,并且TreeView
也支持它。检查该链接。