none
Showing Hierarchical Data in a TreeView RRS feed

  • Question

  • Hi:

    I declared a TreeView in xaml bound to a Hierarchical object in memory, but it doesn't show the info.Here's the object structure:

    public class Animals {
        public string Name { get; set; }
        public List<Kind> Kinds { get; set; }
    }
    
    public class Kind {
        public string Name { get; set; }
        public List<Animal> Animal { get; set; }
    }
    
    public class Animal {
        public string Name { get; set; }
    }

    I initialize it in the Window's constructor:

    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            var animals = new Animals {Name = "Animals Kingdom", Kinds = new List<Kind>()};
    
            var kind = new Kind {Name = "Vertebrae", Animal = new List<Animal>()};
            var animal = new Animal { Name = "Bear" };
            kind.Animal.Add(animal);
            animal = new Animal() { Name = "Tiger" };
            kind.Animal.Add(animal);
            animal = new Animal() { Name = "Lion" };
            kind.Animal.Add(animal);
    
            animals.Kinds.Add(kind);
    
            kind = new Kind { Name = "Invertebrae", Animal = new List<Animal>() };
            animal = new Animal { Name = "Fly" };
            kind.Animal.Add(animal);
            animal = new Animal() { Name = "Grasshopper" };
            kind.Animal.Add(animal);
            animal = new Animal() { Name = "Spider" };
            kind.Animal.Add(animal);
    
            animals.Kinds.Add(kind);
        }
    }

    And I declare it in xaml:

    <Window.Resources>
        <local:Animals x:Key="MyList"/>
    
        <HierarchicalDataTemplate DataType = "{x:Type local:Animals}"
                                ItemsSource = "{Binding Path=Kinds}">
            <TextBlock Text="{Binding Path=Name}"/>
        </HierarchicalDataTemplate>
    
        <HierarchicalDataTemplate DataType = "{x:Type local:Kind}"
                                ItemsSource = "{Binding Path=Animal}">
            <TextBlock Text="{Binding Path=Name}"/>
        </HierarchicalDataTemplate>
    
        <DataTemplate DataType="{x:Type local:Animal}">
            <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
    </Window.Resources>
    <DockPanel>
    
        <TreeView>
            <TreeViewItem x:Name="tree" ItemsSource="{Binding Source={StaticResource MyList}}" Header="Animals" />
        </TreeView>
    
    </DockPanel>

    The TreeView appears empty:

    Any suggestions.

    Rafael


    Believe you can do it, and you will!!

    Saturday, December 14, 2013 8:53 PM

Answers

All replies

  • Sunday, December 15, 2013 6:10 AM
  • Hi Ayyappan:

    Thank you for answering.

    The link Mark As Answer is not present!

    Your reply only appears in the list All Replies, and not as a single Reply

    Can't mark your answer.


    Believe you can do it, and you will!!



    • Edited by RalphDev Sunday, December 15, 2013 5:09 PM
    Sunday, December 15, 2013 4:54 PM
  • Hi Ayyappan:

    Thank you for answering.

    The link Mark As Answer is not present!

    Your reply only appears in the list All Replies, and not as a single Reply

    Can't mark your answer.


    Believe you can do it, and you will!!



    Hi,

    You've posted a "Discussion", so you can't mark an answer. I've changed the type of this thread and mark helpful answer:)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by RalphDev Tuesday, December 17, 2013 12:54 PM
    • Unmarked as answer by RalphDev Tuesday, December 17, 2013 12:56 PM
    • Marked as answer by RalphDev Tuesday, December 17, 2013 12:56 PM
    Monday, December 16, 2013 9:40 AM
    Moderator