Grouping child objects in WPF TreeView

I am trying to get a view of my tree to group a collection of similar elements as they are. To preserve a common property, my object hierarchy might look like this:

  • The objects
    • Group of objects # 1
      • Item # 1 (Type 'A')
      • Item # 2 (Type 'A')
      • Item No. 3 (type 'B')
      • Item # 4 (Type 'B')

Right now, my TreeView shows these objects in exactly the same way as the object model, but I would like to add a TreeView node for each type of object so that it looks like this:

  • The objects
    • Group of objects # 1
      • Type A
        • Paragraph 1
        • Item No. 2
      • Type B
        • Item No. 3
        • Item No. 4

In a similar question, I saw that someone recommended having two separate HierarchicalDataTemplates , so I created one for the Object Group # 1 level, which contains a TreeView with a list of types, but this is really awkward, because it is a separate separate TreeView inside some nodes . I am also trying to use CollectionViewSource to filter out items in each category, but this is not very useful as I cannot figure out how to display them.

I think my question boils down to the following: how to make the HierarchicalDataTemplate group its children? If someone could point me in the right direction, I would really appreciate it.

I can post some code if someone wants to see it, but I'm just trying to figure out how to do what I want, so my code is just a direct tree-like tree-like direct image.

+7
c # wpf xaml treeview hierarchicaldatatemplate
source share
4 answers

Take a look at this article by Mr. Sumi. I am sure this will help you.

+5
source share

You can achieve this effect by IValueConverter ItemsSource to your HierarchicalDataTempalate using IValueConverter . This converter simply does the following:

 public class MyConverter : IValueConverter { public object Convert(object value, ...) { return from item in (IEnumerable<MyItem>)value group item by item.Type into g select new { Type = g.Key, Items = g } } ... } 

Now your HierarchcialDataTemplate might look like this:

 <HierarchicalDataTemplate ItemsSource="{Binding SomePath, Converter={x:Static local:MyConverter}"> <HierarchicalDataTemplate.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Items}" TargetType="{x:Type local:MyItem}" ItemTemplate="{StaticResource MyItemTemplate}"> <!-- may omit ItemTemplate in prior line to use implicit template --> <TextBlock Text="{Binding Type}" /> <!-- Header for type --> </HierarchicalDataTemplate> </HierarchicalDataTemplate.ItemTemplate> <!-- header for "Object Group #1" --> </HierarchicalDataTemplate> 
+7
source share

AFAIK, HierarchicalDataTemplate cannot group its children.

The View should just display everything it gets without digging into the types / groups of objects ... Why don't you create these groups in your object model?

And the view will just get smth like:

 public interface ITreeNode { string Title; IList<ITreeNode> ChildNodes; } 

and display it using the HierarchicalDataTemplate .

0
source share

If this is a simple grouping method from Flat Collection for the display you are looking for, perhaps using "CollectionViewSource" would be more appropriate. Using LINQ can be a nightmare due to the propagation of the Property / Collection Change event.

 <CollectionViewSource x:Key="GroupedItems" Source="{Binding ItemsSource, ElementName=My_UserControl}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="Type" Converter="{StaticResource GroupingConverter}" /> </CollectionViewSource.GroupDescriptions> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="Date"/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> <HierarchicalDataTemplate x:Key="GroupDataTemplate" ItemsSource="{Binding Items}" > <TextBlock Text="{Binding Name}" /> </HierarchicalDataTemplate> <TreeView x:Name="ItemHolder" x:FieldModifier="private" ItemsSource="{Binding Source={StaticResource GroupedItems}, Path=Groups}" ... /> 
0
source share

All Articles