我正在尝试模仿msn messenger联系人列表树视图显示。 我有一个Contact对象,它有一个Groups属性,返回一个联系人所属的数组msngroups,这意味着我有一些东西,如下所示
Class Contact
{
string Name;
string[] Groups {get;set;}
}
ObservableCollection<Contact> ContactList;
所以联系人可以在多个组中,是否可以使用CollectionViewSource生成正确的视图来提供wpf TreeView?
答案 0 :(得分:3)
当您将此层次结构扁平化时,分组将起作用,即“将群组联系”变为“具有每个群组的重复联系人”...
e.g。
如果您有 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>
如果这有帮助,请告诉我......
答案 1 :(得分:1)
////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>
答案 2 :(得分:0)
Bea Stollnitz covered this exact situation. CollectionViewSource
支持定义GroupDescriptions
,TreeView
也支持它。检查链接。