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)
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" }
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.