After some try & error routine :) - I became quite accustomed to XAML, HierarchicalDataTemplate and DataTemplateSelector methodology.
So I would like to share my experiences with you, in case you have problems with getting TreeView to display items correctly.
I have faced 2 main problems:
#1 - Choosing optimal data structure for linking ItemsSource to it
Solved using self referencing data structure
#2 - Writing HierarchicalDataTemplate properly.
Found out that if problem #1 is solved as it should be, this becomes much easier.
Here is what I wanted to display in the TreeView.
As you can see, I wanted to display an unlimited number of categories and products, in a multi-level fashion.
Each category can contain 0...n categories, and also 0...n products.
I have intentionally omitted database details in this article and focused on View part of MVC pattern.
If you are interested in ADO.NET entity mechanism which grabs the information from a database, please let me know - and I will post it here.
After some mistakes - I decided to use the following approach for populating TreeView:
1. Created an self-referencing class TreeDataItem. Kinda like a Tree data structure.
2. Created two subclasses of TreeDataItem:
ProductDataItem for products
CategoryDataItem for categories
3. Made an ObservableCollection