TreeView with multiple HierarchicalDataTemplates

22 views Asked by At

I have three classes

public class Model {
    public string Name {get; set;} 
     public ObservableCollection<Component> Components { get; set; } 
}

public class Component
{
    public string Name {get; set;}
    public ObservableCollection<Component> Children { get; set; }
    public ObservableCollection<Variant> Variants { get; set; }
}

public class Variant
{
    public string Name {get; set;}
    public ObservableCollection<Component> Siblings { get; set; }
}

I wish to generate a HierarchicalDataTemplate to visualize the elements which could be something like this:

- RootComponent
-- FirstComponent
---- FirstVariant
------ FirstVariantFirstComponent
------ FirstVariantSecondComponent
-- SecondComponent

I tried to tweak the following to give me the desired results to no avail ... I need help!

<TreeView Name="treeView" ItemsSource="{Binding Model}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Component}" ItemsSource="{Binding Children}">
            <TextBlock Text="Component" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:Variant}" ItemsSource="{Binding Siblings}">
            <TextBlock Text="Variant" />
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type local:Component">
            <TextBlock Text="Component" />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Variant}">
            <TextBlock Text="Variant" />
        </DataTemplate>
    </TreeView.Resources>
</TreeView>
0

There are 0 answers