0

我正在尝试模仿 msn messenger 联系人列表树视图显示。我有一个联系人对象,它有一个 Groups 属性,该属性返回联系人所属的数组 msngroups,这意味着我有如下内容

Class Contact
{
    string Name;
    string[] Groups {get;set;}
 }

 ObservableCollection<Contact> ContactList;

所以一个联系人可以在多个组中,是否可以使用 CollectionViewSource 生成正确的视图来提供 wpf TreeView?

4

3 回答 3

3

The grouping would work when you flatten this hierarchy i.e. "Contact having Groups" into "Repeated Contacts having each single group" ...

e.g.

If you have 4 items with groups like ...

Dog { mammal, quadruped }
Man { mammal, biped }
PrayingMantis { insect, quadruped }
Pegion { bird, biped }

Then you new flat list should be like this...

<mammal, Dog>
<mammal, Man>
<bird, Pigeon> 
<insect, PrayingMantis>
<biped, Man>
<biped, Pigeon>
<quadruped, Dog>
<quadruped, PrayingMantis>

So after applyin grouping on the Keys above it should be

mammal { Dog, Man }
bird { Pigeon }
insect { PrayingMantis }
biped { Man,  Pigeon }
quadruped { Dog, PrayingMantis }

C# Code:

//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>

Let me know if this helps...

于 2012-08-02T06:41:23.180 回答
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>
于 2013-08-30T10:22:58.300 回答
0

Bea Stollnitz涵盖了这个确切的情况。CollectionViewSource支持定义 ,GroupDescriptionsTreeView支持它。检查那个链接。

于 2012-08-01T16:00:08.790 回答