-4

I have a list of a class looking something like this:

class MyClass
{
   string Group {get;set;}
   string SubGroup {get;set;}
   string Item {get;set;}
}

In my treeview I want the list to be grouped like:

Group1
...SubGroup1
......Item1
......Item 2
...SubGroup2
......Item3
...SubGroup3
......Item4
......Item5
Group2
...SubGroup4
......Item6

What should my xaml look like for that?

I've experimented with nested HierarchicalDataTemplates, GroupStyle and a CollectionViewSource, but nothing seemed to really work....

Also, it would be nice to be able to edit the Item properties.

Edit: has been called a copy of Grouping child objects in WPF TreeView but it seems that this fellow started out with what I want to end up with (sort of)

Community
  • 1
  • 1
h607732
  • 3
  • 7
  • 1
    Possible duplicate of [Grouping child objects in WPF TreeView](http://stackoverflow.com/questions/2248346/grouping-child-objects-in-wpf-treeview) – Quentin Roger Sep 20 '16 at 10:40
  • No, it looks like the guy in that article started out with just about what I want... – h607732 Sep 20 '16 at 10:48

1 Answers1

0

A good code-Samaritan wrote this. I'll post it here, in case others find it useful:

    <DataTemplate x:Key="LeafTemplate">
        <!--your item's property-->
        <TextBlock Text="{Binding Path=Val3}"/>
    </DataTemplate>

    <HierarchicalDataTemplate ItemsSource="{Binding Items}" x:Key="Level2GroupTemplate" ItemTemplate="{StaticResource LeafTemplate}">
        <!--GroupItem.Name-->
        <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate ItemsSource="{Binding Items}" x:Key="Level1GroupTemplate"  ItemTemplate="{StaticResource Level2GroupTemplate}">
        <!--GroupItem.Name-->
        <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>

</Window.Resources>

    public ObservableCollection<TestTreeClass> TestTreeList
    {
        get { return (ObservableCollection<TestTreeClass>)GetValue(TestTreeListProperty); }
        set { SetValue(TestTreeListProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TestTreeList.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TestTreeListProperty =
        DependencyProperty.Register("TestTreeList", typeof(ObservableCollection<TestTreeClass>), typeof(MainWindow), new PropertyMetadata(null));

    TestTreeList = new ObservableCollection<TestTreeClass>()
        {
            new TestTreeClass() { Val1 = "AAA", Val2 = "111", Val3 = "abc" },
            new TestTreeClass() { Val1 = "AAA", Val2 = "111", Val3 = "def" },
            new TestTreeClass() { Val1 = "BBB", Val2 = "111", Val3 = "ghi" },
            new TestTreeClass() { Val1 = "BBB", Val2 = "111", Val3 = "jkl" },
            new TestTreeClass() { Val1 = "AAA", Val2 = "222", Val3 = "mno" }
};
h607732
  • 3
  • 7