wpf treeview does not show child objects

Posted by gangt on Stack Overflow See other posts from Stack Overflow or by gangt
Published on 2010-06-03T01:00:45Z Indexed on 2010/06/03 1:04 UTC
Read the original article Hit count: 218

Filed under:
|

I have an object with child object(s) and I load it using linq. And I assign it to a treeView's itemssource.

treeView.DisplayMemberPath = "Name";
treeView.ItemsSource = tasks;

It shows only the parent nodes (task.name), I couldn't figure out how to add children (TaskItems.name). All the examples show HierarchicalData in xaml. I need to do it in code-behind, just like the above code. Is it possible?


public class Task
{
        public int Id;
        public string Name;
        public bool IsActive;

        public List<TaskItem> TaskItems = new List<TaskItem>();
}

public class TaskItem
{
        public int TaskId;
        public string Name;
        public string Value;
}

--------------

var tasks1 = from t in xd.Descendants("taskheader")
            select new Task
            {
                Id = (int)t.Element("id"),
                Name = t.Element("name").Value,
                IsActive = t.Element("isactive").Value == "1",
                TaskItems = t.Elements("taskdetail").Select(e => new TaskItem
                {
                    TaskId = (int)e.Element("taskid"),
                    Name = (string)e.Element("name"),
                    Value = (string)e.Element("value"),
                }).ToList()
            };

--------------
List<Task> tasks = new List<Task>();
tasks = tasks1;

© Stack Overflow or respective owner

Related posts about wpf

Related posts about treeview