For the scenario - Organisation having different department, each department having different employees, the hierarchical data template works perfectly.
How about a scenario like Question Paper (Parent cluster) having both child clusters (Question item group) as well as Question Items. Like as follows:
Bilogy (Parent Cluster)
- Group 1 (Child cluster)
- Question 1(Question Item)
- Question 2(Question Item)
- Group 2 (Child Cluster)
- Sub Group 1(Child Cluster) 开发者_如何学运维
- Question 1(Question Item)
- Question 2(Question Item)
Here I have two entities - Cluster and Question Item. Cluster can contain list of clusters as well as list of answer items.
Basically issue comes as the same hierarchical data template for cluster should bind to two item sources, one for child clusters and one for question items. Is there a way to use hierarchical data template in this case so that I can use tree view for this.
I am using SIlverlight 4.0.
Yes, you should be able to achieve that with HeirarchicalDataTemplates
.
You can either create a HeirarchicalDataTemplates
for ParentCluster, ChildCluster and then use a normal DataTemplate
for the Question.
<HierarchicalDataTemplate DataType="{x:Type ParentClusterViewModel}" ItemsSource="{Binding ChildItems}">
<ParentClusterView />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ChildClusterViewModel}" ItemsSource="{Binding ChildItems}">
<ChildClusterView />
</HierarchicalDataTemplate>
If you then have a base class 'TreeItemBase' that all your tree ViewModels inherit from, then you'll be able to do what you want.
public abstract class TreeItemBase
{
public List<TreeItemBase> ChildItems { get; set; }
}
public class ParentClusterViewModel : TreeItemBase { ... }
public class ChildClusterViewModel : TreeItemBase { ... }
public class QuestionViewModel : TreeItemBase { ... }
Found a solution for this. Basically both Cluster and QuestionItem are type of WorkItem. I abstracted them to WorkItem and used a colletion of workitem in cluster which returns combined list of QuestionItems andsub clusters. In the TemplateSelector overridden SelectTemplate to provide the Hierarchical data template/ data template based on the Work Item type.
精彩评论