1

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

Telefisch
  • 305
  • 1
  • 19
  • Sorry my last comment was wrong. Because persons is a List and Children is the interface IList you should be okay with just _Children = persons_ without anything else? – Ben May 18 '20 at 13:33
  • What `Children = new...` do you want to replace? The `Children` can only be set to an `IList`. It doesn't matter where this comes from. – mm8 May 18 '20 at 13:39
  • Oh yes, I thought also (and tried serveral approaches, also with `IList`). What I think is confusing, Children is a readonly property but with the "static Children" it works, with the IList it doesn't (because of readonly). Can someone explain the difference, please? – Telefisch May 19 '20 at 06:16

1 Answers1

1

What I think is confusing, Children is a readonly property but with the "static Children" it works, with the IList it doesn't (because of readonly). Can someone explain the difference, please?

Your GetFamilyTree() method uses a nested object initializer. Please refer to Jon Skeet's answer here for more information.

If you intend to read data from some source and then set the Children property, you should add a setter to it. You can also remove the backing field:

public class Person
{
    public IList<Person> Children { get; set; } = new List<Person>();
    public string Name { get; set; }
}
mm8
  • 163,881
  • 10
  • 57
  • 88