I have scenario where my TreeView look like below:
- MainParent
- .>-Parent
- ...>--Child1
- ...>--Child2
- ...>--Child3
- ...>--Child4
- ...>--Child5
- ......> Child1_Of_Child5
- ...........>Child1(Child1_Of_Child5)
- ...........>Child2(Child1_Of_Child5)
- ......> Child2_of_Child5
- ...........>Child1(Child2_Of_Child5)
- ...........>Child2(Child2_Of_Child5)
I am able to bind ObservableCollection till Child1/Child2/../Child5 using HierarchicalDataTemplate. Though I am able to add Child1_Of_Child5 to Child5 ObservableCollection (seen in debug mode), Same is not reflecting in UI/Treeview.
Below is my XAML code:
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type VM:ProjectViewModel}" ItemsSource="{Binding SelectActivityViewModelCollection}">
<StackPanel Orientation="Horizontal">
<Image Source="./resources/New_Package.png" Width="15" Height="15"/>
<TextBlock Text="{Binding Header}"></TextBlock>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type VM:SelectActivityViewModel}" ItemsSource="{Binding ToolsViewModelCollection}"
>
<StackPanel Orientation="Horizontal">
<Image Source="./resources/New_Package.png" Width="15" Height="15"/>
<TextBlock Text="{Binding Header}"></TextBlock>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type VM:ToolsViewModel}" ItemsSource="{Binding SheetsViewModelBaseCollection}">
<StackPanel Orientation="Horizontal">
<Image Source="./resources/New_Package.png" Width="15" Height="15"/>
<TextBlock Text="{Binding Header}"></TextBlock>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate x:Name="LastLevel" DataType="{x:Type VM:ViewModelBase}" ItemsSource="{Binding FeaturesViewModelCollection}">
<StackPanel Orientation="Horizontal">
<Image Source="./resources/file.png" Width="15" Height="15"/>
<TextBlock Text="{Binding Header}"/>
</StackPanel>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
Class Structure: (ViewModelBase is extended to all classes)
- ProjectViewModel - has SelectActivityViewModelCollection = (ObservableCollection(SelectActivityViewModel))
- SelectActivityViewModel - has ToolsViewModelCollection = (OC(ToolsViewModel))
- ToolsViewModel - has SheetsViewModelBaseCollection and FeaturesViewModelCollection = (OC(ViewModelBase) and OC(FeaturesViewModel))
- FeaturesViewModel - has UseCaseViewModelCollection = (OC(UseCaseViewModel))
- In ToolsViewModel I am using SheetsViewModelBaseCollection to store Child1/Child2/.../Child5
- and FeaturesViewModelCollection - to store Child1_Of_Child5/Child2_Of_Child5/../Child(n)_Of_Child5
EDIT : As i have extended ViewModelBase to all classes, i am using OC(ViewModelBase) to store different sheets(.i.e.,in SheetsViewModelCollection). Note:
- No DataBase is used: Serialisation and Deserialisation is used
- WPF : MVVM pattern is followed