I'm just trying to understand an example of WPF treeview.
My goal ist to populate a treeview with some items, stored in a List.
This is the example:
public class Person
{
readonly List<Person> _children = new List<Person>();
public IList<Person> Children
{
get { return _children; }
}
public string Name { get; set; }
}
//Some added nodes:
public static Person GetFamilyTree()
{
// In a real app this method would access a database.
return new Person
{
Name = "David Weatherbeam",
Children =
{
new Person
{
Name="Alberto Weatherbeam",
Children=
{
new Person
{
Name="Zena Hairmonger",
Children=
{
new Person
{
Name="Sarah Applifunk",
}
}
},
new Person
{
Name="Jenny van Machoqueen",
Children=
{
new Person
{
Name="Nick van Machoqueen",
},
new Person
{
Name="Matilda Porcupinicus",
},
new Person
{
Name="Bronco van Machoqueen",
}
}
}
}
}
}
};
}
So far it works.
Now I'd like to replace the static persons unter the first parent node with a list that I created before, reading a file.
XDocument ecad = XDocument.Load(openFileDialog.FileName);
GlobalVar.persons = new List<Persons>(); //globally available list
Person person = null;
//Einlesen der XML-Datei in lokale Variable (Model)
foreach(XElement elem in ecad.Descendants("Variable"))
{
if (elem.Element("Name") != null)
{
person = new Person(){ Name = elem.Element("Name").Value };
persons.Add(person);
}
}
Now I'd like to add these Persons to a root-person
//GlobalVar.List<Persons> persons
public static Person GetFamilyTree()
{
return new Person{
Name = "Family",
Children = persons
}
}
So how can I replace the Children = new...
with the function that reads the data from file?
I'm really confused