There are some simple rules when the HierarchicalDataTemplate is used
A. If the described item has subitems/children then we are using the form of HierarchicalDataTemplate;
B. If the
described item is a leaf (i.e. without children by design) we do not need the HierarchicalDataTemplate
form but just the DataTemplate
To demonstrate the case deeply I modified the example. I put two trees in the page in order to compare the usage of item templates. Both trees are using the same DataSoutce object but the visual result is quite different. I changed some of property and template names in order to make the code more straightforward(batteryTemplate, batteryList, etc.). I put some comments inside the xaml file to divide the code related to the first and the second trees. Here is a brief description of what I done and why:
- The data source - equal for both trees.
- The first Tree - myTree:
- we are using two HierarchicalDataTemplat for first two level of the tree because they have sub items;
- we skipped the usage of DataTemplate for the third level items because we not set the ItemTemplate in batteryTemplate and the item's text is bound to the same named properties - Name. Please note that the Foreground color and the Italic modification are applied too.
- The second Tree - myTree2:
- we are using item templates for all items: areaTemplate2, batteryTemplate2 and wellTemplate2.
- note in batteryTemplate2we initialized the ItemTemplate.
- the wellTemplate2 is of type DataTemplate - remember the simple rules at the beginning.
Please note the trick from the 2.2. point is not a good practice because we rely on the same named properties but if we changed for example the name of Well.Name to the Well.WellName (i put the code for this case in comments in both xaml and cs files) than the text from the well-items will disappear.
All the best,
the Telerik team
Check out Telerik Trainer
, the state of the art learning tool for Telerik products.