To Question 1: ObservableCollections has the advantage that all notifications for presentation are automatically executed. Therefore, you do not need to worry about NotifyPropertyChanged events to add / remove objects from the collection.
To Question 2: Is your ViewModel your DataContext ? I donβt see where the ObservableCollection<ICustomer> property is? Can you provide more information about relationships in these classes?
EDIT:
Based on mm8's answer, the x: Type attribute must have a specific type. Therefore, the code should look something like this:
I suggest you have an ObservableCollection<ICustomer> ListOfCustomers in your ViewModel , then you can bind it in the view:
<TreeView ItemsSource="{Binding ListOfCustomers}"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type Customer}"> <TextBlock Text="{Binding Id}"/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type modSettings:Value}" ItemsSource="{Binding ListOfValues}"> <TextBlock Text="{Binding Name}"/> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView>
I think this thread provides a more detailed solution to your problem: Implement the WPF tree with different parent nodes in the same way as other child nodes?
EDIT2:
I modified my code a bit to fit your requirements. This should display all nodes:
<TreeView ItemsSource="{Binding ListOfCustomers}" > <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path=ListOfValues}" DataType="{x:Type Customer}"> <HierarchicalDataTemplate.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type modSettings:Value}"> <TextBlock Text="{Binding Path=Name}"/> </HierarchicalDataTemplate> </HierarchicalDataTemplate.ItemTemplate> <TextBlock Text="{Binding Path=Id}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
source share