0

I am trying to mimic the msn messenger contactlist treeview display. I have a Contact object which has a Groups property that returns an array msngroups that the Contact belongs, this meaning say I have something as per bellow

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

 ObservableCollection<Contact> ContactList;

So a contact can be in mutiple groups, is that possible to use CollectionViewSource to generate the correct view to feed a wpf TreeView?

tesla1060
  • 2,621
  • 6
  • 31
  • 43

3 Answers3

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

WPF-it
  • 19,625
  • 8
  • 55
  • 71
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>
  • 1
    if your CollectionViewSource.GroupDescriptions.GroupDescription return object derived from ICollection, WPF framework will enumerate every item in it and add the item to group list – user2732546 Aug 30 '13 at 10:28
0

Bea Stollnitz covered this exact situation. The CollectionViewSource supports defining GroupDescriptions, and TreeView also supports it. Check that link out.

erodewald
  • 1,815
  • 20
  • 45